Shiro缓存失效以后的一个问题

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

shiro 1.2.2和1.2.3

为shiro设置了缓存,但是当服务器运行几个小时后,页面判断

<shiro:haspermission name="admin">

<li class="mail">有权限

</li>

</shiro:hasPermission>

一直未显示。重新登陆也无效。判断问题应该是,实际缓存失效了,但是框架仍然认为有效。

尝试无效办法

(1)

倘若把shiro对应的ehcache配置文章,该掉设置,

timeToIdleSeconds="10"

timeToLiveSeconds="10"

该问题依旧出现。但出问题频次减少

(2)在application-shiro里面添加

<bean id="sessionManager"

class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">

<!-- 超时时间 -->

<PRoperty name="globalSessionTimeout" value="3600" />

<property name="sessionDAO" ref="sessionDAO" />

<!-- 定时检查失效的session -->

<property name="sessionValidationSchedulerEnabled" value="true" />

</bean>

<bean id="sessionDAO"

class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">

<property name="activeSessionsCacheName" value="shiro-activeSessionCache" />

</bean>

依旧没用

google到 http://stackoverflow.com/questions/17657283/cache-invalidate-not-working-in-shiro

(3)验证权限的时候,主动清除缓存。

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)

throws AuthenticationException {

...

SimplePrincipalCollection principals = new SimplePrincipalCollection(username, "jndiJdbcRealm");

super.doClearCache(principals);

...

}

最后,在日志里面发现

15:31:24.379 [main] DEBUG o.a.shiro.realm.AuthorizingRealm - No authorizationCache instance set. Checking for a cacheManager...

15:31:24.379 [main] INFO o.a.shiro.realm.AuthorizingRealm - No cache or cacheManager properties have been set. Authorization cache cannot be obtained.

15:31:24.405 [main] DEBUG o.a.s.s.LifecycleBeanPostProcessor - Initializing bean [shiroEhcacheManager]...

在shiroDbRealm里面添加这一句

<property name="cacheManager" ref="shiroEhcacheManager" />

问题依旧无解

终极解决

1、

log配置文件里面添加

<logger name="org.apache.shiro" level="trace" >

<appender-ref ref="STDOUT" />

</logger>

之后得到日志信息

22:48:20.765 [http-80-3] DEBUG o.a.shiro.realm.AuthenticatingRealm - AuthenticationInfo caching is disabled for info [null].

加上以下内容以后,依旧无效。

<property name="authenticationCachingEnabled" value="true" />

<property name="authorizationCachingEnabled" value="true" />

根源在ShiroUser对象的tostring方法,用的是loginName,但由于业务不需要,loginName根本就没有赋值。所以字符串“NULL”是缓存的key。

故当所有人登录以后,保存的cache key是“null”,一直会互相覆盖。

改写ShiroUser的Tostring()方法,用系统唯一值登录名赋值。问题解决

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航