shiro中提供了对认证和授权的缓存,shiro是默认开始授权缓存而关闭认证缓存的
在SecurityManager中需要这个参数
项目一
- <!-- 定义Shiro安全管理配置 -->
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <property name="realm" ref="systemAuthorizingRealm" />
- <property name="sessionManager" ref="sessionManager" />
- <property name="cacheManager" ref="shiroCacheManager" />
- </bean>
- <!-- securityManager安全管理器 -->
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <property name="realm" ref="customRealm" />
- <!-- 注入缓存管理器 -->
- <property name="cacheManager" ref="cacheManager"/>
- <!-- 注入session管理器 -->
- <property name="sessionManager" ref="sessionManager" />
- <!-- 记住我 -->
- <property name="rememberMeManager" ref="rememberMeManager"/>
- </bean>
cacheManager
- <!-- 缓存管理器 -->
- <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
- <property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/>
- </bean>
- <bean id="shiroCacheManager" class="com.minstone.common.security.shiro.cache.RedisCacheManager">
- <property name="redisManager" ref="redisManager" />
- </bean>
redisManager 就是一个简单的数据库连接
- <bean id="redisManager" class="com.minstone.common.utils.redis.RedisManager">
- <!-- 连接池配置 -->
- <property name="jedisPoolConfig" ref="jedisPoolConfig"></property>
- <!-- Redis服务主机 -->
- <property name="host" value="${redis.host}"></property>
- <!-- Redis服务端口号 -->
- <property name="port" value="${redis.port}"></property>
- <!-- 连超时设置 -->
- <property name="timeout" value="${redis.timeout}"></property>
- <!-- 是否使用连接池 -->
- <property name="usePool" value="${redis.usePool}"></property>
- <!-- Redis服务连接密码 -->
- <property name="password" value="${redis.password}"></property>
- </bean>
缓存清空
如果用户正常退出 缓存清空
如果用户非正常退出比如关闭浏览器 缓存清空
如果session到期,缓存清空,设置一般在缓存的配置文件中
如果修改了用户的权限,而用户不退出系统,修改的权限不会生效,需要手动调用realm的clearCache方法清除
- public void clearCached() {
- PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
- super.clearCache(principals);
- }
- public static void remove(String cacheName, String key) {
- / getCache(cacheName).remove(key);
- RedisUtils.removeCache(key);
- }
- public static void removeCache(String key){
- / LOGGER.debug("根据key从redis中删除对象 key [" + key + "]");
- cacheManager.del(key.getBytes());
- }
还可以在SecurityManager中配置Sessionmanager,配制方法增加property属性
- <!-- 自定义会话管理配置 -->
- <bean id="sessionManager" class="com.common.security.shiro.session.SessionManager">
- <property name="sessionDAO" ref="sessionDAO"/>
- <!-- 会话超时时间,单位:毫秒 -->
- <property name="globalSessionTimeout" value="${session.sessionTimeout}"/>
- <!-- 定时清理失效会话, 清理用户直接关闭浏览器造成的孤立会话 -->
- <property name="sessionValidationInterval" value="${session.sessionTimeoutClean}"/>
- lt;!-- <property name="sessionValidationSchedulerEnabled" value="false"/> -->
- <property name="sessionValidationSchedulerEnabled" value="true"/>
- <property name="sessionIdCookie" ref="sessionIdCookie"/>
- <property name="sessionIdCookieEnabled" value="true"/>
- </bean>
- <!-- 会话管理器 -->
- <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
- <!-- session的失效时长,单位毫秒 -->
- <property name="globalSessionTimeout" value="600000"/>
- <!-- 删除失效的session -->
- <property name="deleteInvalidSessions" value="true"/>
- </bean>
shiro管理session,shiro提供了sessionDao操作
- <!-- 自定义Session存储容器 -->
- <bean id="sessionDAO" class="com.minstone.common.security.shiro.session.CacheSessionDAO">
- <property name="sessionIdGenerator" ref="idGen" />
- <property name="activeSessionsCacheName" value="activeSessionsCache" />
- <property name="cacheManager" ref="shiroCacheManager" />
- </bean>
- 转载地址:https://blog.csdn.net/oppoppoppo/article/details/53432202