shiro缓存管理和session管理

本文介绍了Apache Shiro框架中的缓存和Session管理。Shiro默认开启授权缓存并关闭认证缓存,可以通过SecurityManager配置cacheManager。当用户正常或非正常退出时,缓存会被清空。此外,Session到期也会导致缓存清除。若要即时更新用户权限,需手动调用Realm的clearCache方法。同时,可在SecurityManager中配置SessionManager以定制更多属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
redis

	<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);
	}
redis

	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());
	}
注意 这些方法一般情况下修改用户权限都是在Service当中所以应该在service中调用清空缓存的方法

还可以在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}"/>
<!--  		<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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值