[关闭]
@wxf 2017-11-07T01:12:29.000000Z 字数 10204 阅读 893

Shiro权限管理整合

项目实战


整合shiro权限管理

在上面搭建好的框架基础上整合权限模块。首先添加shiro-alljar包,jar包地址如下:

  1. <dependency>
  2. <groupId>org.apache.shiro</groupId>
  3. <artifactId>shiro-all</artifactId>
  4. <version>1.3.2</version>
  5. </dependency>

配置shiroFilter过滤器

在web系统中,shiro是通过filter进行拦截。所以需要在web.xml中添加shiroFilter过滤器,配置信息如下:

  1. <!-- 配置shiro的filter -->
  2. <filter>
  3. <filter-name>shiroFilter</filter-name>
  4. <!-- shiro过滤器,DelegatingFilterProxy通过代理模式将spring容器汇中的bean和filter关联起来 -->
  5. <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  6. <!-- 设置true,表示由servlet容器控制filter的生命周期 -->
  7. <init-param>
  8. <param-name>targetFilterLifecycle</param-name>
  9. <param-value>true</param-value>
  10. </init-param>
  11. <!--<init-param>-->
  12. <!--<param-name>transformWsdlLocations</param-name>-->
  13. <!--<param-value>true</param-value>-->
  14. <!--</init-param>-->
  15. <!-- 设置spring容器filter的bean id,如果不设置则找与filter-name一致的bean -->
  16. <init-param>
  17. <param-name>targetBeanName</param-name>
  18. <param-value>shiroFilter</param-value>
  19. </init-param>
  20. </filter>
  21. <filter-mapping>
  22. <filter-name>shiroFilter</filter-name>
  23. <url-pattern>/*</url-pattern>
  24. </filter-mapping>

DelegatingFilterProxy 实际上是 Filter 的一个代理对象. 默认情况下, Spring 会到 IOC 容器中查找和<filter-name>对应的 filter Bean。也可以通过 targetBeanName 的初始化参数来配置 filter Bean 的 id。其实简单来说就是:filter拦截后将操作权交给了在spring中配置的filterChain(过滤链)

与进行Spring整合

-----以上配置就是shiro与spring整合时最基础的配置-----

配置登录退出

使用FormAuthenticationFilter过滤器实现,参看过滤器源码得知原理如下:
1.认证时请求数据是如何被捕获的?
当用户没有认证时,请求会跳转到loginurl进行认证,然后FormAuthenticationFilter过滤器拦截并取出request请求中的username和password(两个参数名称是可以配置的)。
2.获得参数后过滤器是如何进行认证的?
FormAuthenticationFilter拦截器调用realm(CustomRealm)的doGetAuthenticationInfo并传入一个token(username和password),然后realm认证时根据username查询用户信息,如果查询不到realm返回null,FormAuthenticationFilter向request域中填充一个参数(记录异常信息)。

用户认证

权限授权

使用PermissionsAuthorizationFilter拦截器

注解授权

jsp标签授权

  1. <%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>
标签名称 标签条件
<shiro:authenticated> 登录之后
<shiro:notAuthenticated> 不在登录状态时
<shiro:guest> 用户在没有RememberMe时
<shiro:user> 用户在RememberMe时
<shiro:hasAnyRoles name="abc,123" > 在有abc或者123角色时
<shiro:hasRole name="abc"> 拥有角色abc
<shiro:lacksRole name="abc"> 没有角色abc
<shiro:hasPermission name="abc"> 拥有权限资源abc
<shiro:lacksPermission name="abc"> 没有abc权限资源
<shiro:principal property="username"/> 显示用户身份中的属性值

shiro缓存

shiro中提供了对认证信息和授权信息的缓存。shiro默认是关闭认证信息缓存的。对于授权信息的缓存默认是开启的。授权信息的缓存作为主要研 究对象。在这里我们使用ehcache进行数据缓存。

  1. <!-- 配置 SecurityManager 安全管理器 -->
  2. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  3. <property name="realm" ref="customRealm" />
  4. <!-- 注入缓存管理器 -->
  5. <property name="cacheManager" ref="cacheManager" />
  6. </bean>
  7. <!-- 缓存管理器 -->
  8. <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
  9. <property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml" />
  10. </bean>
  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
  3. <!--diskStore:缓存数据持久化的目录 地址 -->
  4. <diskStore path="F:\develop\ehcache" />
  5. <defaultCache
  6. maxElementsInMemory="1000"
  7. maxElementsOnDisk="10000000"
  8. eternal="false"
  9. overflowToDisk="false"
  10. diskPersistent="false"
  11. timeToIdleSeconds="120"
  12. timeToLiveSeconds="120"
  13. diskExpiryThreadIntervalSeconds="120"
  14. memoryStoreEvictionPolicy="LRU">
  15. </defaultCache>
  16. </ehcache>

1.如果用户正常退出,缓存自动清除。
2.如果用户非正常退出,缓存自动清空。
3.如果修改了用户的权限,而用户不退出系统,修改的权限无法立即生效。若要实现立即生效功能,需要手动进行编程实现。方法如下:

在权限修改后调用realm的clearCache方法清除缓存。(下边的代码正常开发时要放在service中调用)

  1. //清除缓存(该方法需要在realm中进行定义)
  2. public void clearCached() {
  3. PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
  4. super.clearCache(principals);
  5. }

shiro的会话管理器

和shiro整合后,使用shiro的session管理,shiro提供sessionDao操作会话数据。

  1. <!-- 配置 SecurityManager 安全管理器 -->
  2. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  3. <property name="realm" ref="customRealm" />
  4. <property name="cacheManager" ref="cacheManager" />
  5. <!-- 注入会话管理器 -->
  6. <property name="sessionManager" ref="sessionManager"/>
  7. </bean>
  8. <!-- 会话管理器 -->
  9. <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
  10. <property name="globalSessionTimeout" value="600000"/>
  11. <property name="deleteInvalidSessions" value="true"/>
  12. </bean>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注