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()方法,用系統唯一值登錄名賦值。問題解決
新聞熱點
疑難解答