分享

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

 WindySky 2017-06-16

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:///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()方法,用系统唯一值登录名赋值。问题解决

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多