shiro开发,shiro的环境配置(基于spring+springMVC+redis)

特别感谢 lhacker分享的文章,对我帮助很大

http://www.aiuxian.com/article/p-1913280.html



基本的知识就不在这里讲了,在实战中体会shiro的整体设计理念


首先,大体的了解了一下shiro,发现shiro自带的所有功能并不能满足真正的开发需求,决定自定义部分功能。


在自定义之前,先把web.xml配置好

<!-- shiro配置文件 -->
  	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:shiro-context.xml
		</param-value>
	</context-param>
	<!-- spring上下文监听 -->
  <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	 <!-- shiro过滤器-->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
 <filter-mapping> 
   <filter-name>shiroFilter</filter-name> 
   <url-pattern>/*</url-pattern> 
 </filter-mapping>
 
 <!-- springMVC字符编码过滤器 -->
  <filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
 
  <!-- springMVC -->
  <servlet><servlet-name>action</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
 	<session-config>
		<session-timeout>20</session-timeout>
	</session-config>


1、自定义shiro的验证

shiro的验证并不能实现我想要的功能,我想自己实现我想要的功能,这里自定义role验证,新建一个

AnyRolesAuthorizationFilter继承AuthorizationFilter,重写 isAccessAllowed方法,这个类会在<strong style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">下面是自定义role权限验证的配置</strong>

<!-- 定义 web 支持的 SecurityManager 和'shiroFilter' bean 将会被 web.xml 引用-->
	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<!-- 自定义url过滤 -->
		<property name="securityManager" ref="securityManager" />
		<!-- 自定义role验证 -->
	 <property name="filters">
            <map>
                <entry key="anyRoles" value-ref="anyRolesAuthorizationFilter" />
            </map>
        </property>
        <property name="filterChainDefinitions">
            <value>
               /edit.html = anyRoles[admin]
                 /** = anon
            </value>
        </property>
	</bean>
<!-- 自定义role验证的实现类 -->
<span style="white-space:pre">	</span><bean id="anyRolesAuthorizationFilter" class="cn.wo2306.bbs.shiro.service.AnyRolesAuthorizationFilter"></bean>
/**
 * 自定义角色权限验证
 * @author eguid
 *
 */
public class AnyRolesAuthorizationFilter extends AuthorizationFilter{


	@Override
	protected boolean isAccessAllowed(ServletRequest req,
			ServletResponse res, Object arg2) throws Exception {
}
}


2、自定义授权和缓存管理

我们要自定义授权管理和缓存管理

其中,

授权管理由realm定义,用于管理用户授权;这里我们自定义一个类MyShiroRealm用于重写realm授权。

缓存管理,主要用于管理shiro内部对session的管理,我们需要session放到redis数据库里,所以这里也要重写

我们使用CustomShiroCacheManager自定义session管理,这个管理器需要用到数据库的操作,所以我们用shiroCacheManager类来处理数据的增删改查业务,redisManager是已经封装好的redis数据库操作包


<!-- 配置安全管理器 -->

<span style="font-size:14px;"><strong>	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="sessionManager" ref="defaultWebSessionManager" />
		<!-- 不使用自带的授权管理器,使用自定义授权管理器-->
		<property name="realm" ref="myShiroRealm"></property>
		<!-- 自定义缓存管理器 -->
		<property name="cacheManager" ref="customShiroCacheManager" />
	</bean>

	<!-- 自定义缓存管理 -->
	<bean id="customShiroCacheManager" class="cn.wo2306.bbs.shiro.service.CustomShiroCacheManager">
		<property name="shiroCacheManager" ref="shiroCacheManager"></property>
	</bean>
	<bean id="shiroCacheManager" class="cn.wo2306.bbs.shiro.service.ShiroCacheManager">
	<property name="redisManager" ref="redisManager"></property>
	</bean>
	<bean id="redisManager" class="cn.wo2306.bbs.util.redisUtil.RedisManager"></bean>
	
	<!-- 自定义session -->
	<bean id="defaultWebSessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
		<property name="globalSessionTimeout" value="1200000" />
	</bean>

	<!-- 自定义授权管理器 -->
	<bean id="myShiroRealm" class="cn.wo2306.bbs.shiro.realms.ShiroDbRealm">
		<property name="accountService" ref="accountService" />
	</bean>
	<bean id="accountService" class="cn.wo2306.bbs.shiro.service.AccountService"></bean>
</strong></span>

配置完这些,你会发现,真正要用shiro,你得把全部实现类重写才能满足我们的需求,可是这个简单需求我完全可以用aop直接实现,而不需要shiro框架;而复杂的权限,shiro根本没办法控制;所以到此为止,给shiro得出结论就是简单但是不通用,不适合复杂权限管理,于是果断放弃shiro。

至于选用什么框架适合,我觉得如果是简单权限管理完全不需要框架(可以采用jdk自带secruity),复杂权限可以采用spring-secruity,有实力可以自行编写一个权限管理框架(个人推荐自行编写,不需要依赖其他框架)。

原文地址:https://www.cnblogs.com/eguid/p/6821623.html