shiro缓存管理和session管理

shiro中提供了对认证和授权的缓存,shiro是默认开始授权缓存而关闭认证缓存的

在SecurityManager中需要这个参数

项目一

[html]  view plain  copy
  1. <!-- 定义Shiro安全管理配置 -->  
  2. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
  3.     <property name="realm" ref="systemAuthorizingRealm" />  
  4.     <property name="sessionManager" ref="sessionManager" />  
  5.     <property name="cacheManager" ref="shiroCacheManager" />  
  6. </bean>  
项目二
[html]  view plain  copy
  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.         <!-- 注入session管理器 -->  
  7.         <property name="sessionManager" ref="sessionManager" />  
  8.         <!-- 记住我 -->  
  9.         <property name="rememberMeManager" ref="rememberMeManager"/>  
  10.           
  11.     </bean>  
可以使用不同的缓存

cacheManager

[html]  view plain  copy
  1. <!-- 缓存管理器 -->  
  2. <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">  
  3.         <property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/>  
  4.     </bean>  
redis

[html]  view plain  copy
  1. <bean id="shiroCacheManager" class="com.minstone.common.security.shiro.cache.RedisCacheManager">  
  2.     <property name="redisManager" ref="redisManager" />  
  3. </bean>  

redisManager 就是一个简单的数据库连接

[html]  view plain  copy
  1. <bean id="redisManager" class="com.minstone.common.utils.redis.RedisManager">  
  2.    <!-- 连接池配置 -->  
  3.     <property name="jedisPoolConfig" ref="jedisPoolConfig"></property>  
  4.     <!-- Redis服务主机 -->  
  5.     <property name="host" value="${redis.host}"></property>  
  6.     <!-- Redis服务端口号 -->  
  7.     <property name="port" value="${redis.port}"></property>  
  8.     <!-- 连超时设置 -->  
  9.     <property name="timeout" value="${redis.timeout}"></property>  
  10.     <!-- 是否使用连接池 -->  
  11.     <property name="usePool" value="${redis.usePool}"></property>  
  12.     <!-- Redis服务连接密码 -->  
  13.     <property name="password" value="${redis.password}"></property>  
  14. </bean>  

缓存清空

如果用户正常退出 缓存清空

如果用户非正常退出比如关闭浏览器 缓存清空

如果session到期,缓存清空,设置一般在缓存的配置文件中

如果修改了用户的权限,而用户不退出系统,修改的权限不会生效,需要手动调用realm的clearCache方法清除

[java]  view plain  copy
  1. public void clearCached() {  
  2.     PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();  
  3.     super.clearCache(principals);  
  4. }  
redis

[java]  view plain  copy
  1. public static void remove(String cacheName, String key) {  
  2. /       getCache(cacheName).remove(key);  
  3.     RedisUtils.removeCache(key);  
  4. }  
[java]  view plain  copy
  1. public static void removeCache(String key){  
  2. /       LOGGER.debug("根据key从redis中删除对象 key [" + key + "]");  
  3.     cacheManager.del(key.getBytes());  
  4. }  
注意 这些方法一般情况下修改用户权限都是在Service当中所以应该在service中调用清空缓存的方法

还可以在SecurityManager中配置Sessionmanager,配制方法增加property属性

[html]  view plain  copy
  1. <!-- 自定义会话管理配置 -->  
  2. <bean id="sessionManager" class="com.common.security.shiro.session.SessionManager">   
  3.     <property name="sessionDAO" ref="sessionDAO"/>  
  4.       
  5.     <!-- 会话超时时间,单位:毫秒  -->  
  6.     <property name="globalSessionTimeout" value="${session.sessionTimeout}"/>  
  7.       
  8.     <!-- 定时清理失效会话, 清理用户直接关闭浏览器造成的孤立会话   -->  
  9.     <property name="sessionValidationInterval" value="${session.sessionTimeoutClean}"/>  
  10. lt;!--          <property name="sessionValidationSchedulerEnabled" value="false"/> -->  
  11.         <property name="sessionValidationSchedulerEnabled" value="true"/>  
  12.           
  13.     <property name="sessionIdCookie" ref="sessionIdCookie"/>  
  14.     <property name="sessionIdCookieEnabled" value="true"/>  
  15. </bean>  

[html]  view plain  copy
  1. <!-- 会话管理器 -->  
  2.     <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">  
  3.         <!-- session的失效时长,单位毫秒 -->  
  4.         <property name="globalSessionTimeout" value="600000"/>  
  5.         <!-- 删除失效的session -->  
  6.         <property name="deleteInvalidSessions" value="true"/>  
  7.     </bean>  

shiro管理session,shiro提供了sessionDao操作

[html]  view plain  copy
  1. <!-- 自定义Session存储容器 -->  
  2. <bean id="sessionDAO" class="com.minstone.common.security.shiro.session.CacheSessionDAO">  
  3.     <property name="sessionIdGenerator" ref="idGen" />  
  4.     <property name="activeSessionsCacheName" value="activeSessionsCache" />  
  5.     <property name="cacheManager" ref="shiroCacheManager" />  

  1. </bean>  
  2. 转载地址:https://blog.csdn.net/oppoppoppo/article/details/53432202

猜你喜欢

转载自blog.csdn.net/m0_38053538/article/details/80430696