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

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

转载于:https://www.cnblogs.com/clarkapp/p/4922998.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值