问题描述
使用shiro进行权限认证
并且,使用了shiro的缓存管理,ehcache.xml配置如下
<?xml version="1.0" encoding="UTF-8"?>
<ehcache name="shiroCache"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"><!-- <diskStore path="java.io.tmpdir" /> --><diskStore path="C:\test" /><defaultCache maxElementsInMemory="10000"maxElementsOnDisk="0" eternal="true" overflowToDisk="true"diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="0"diskSpoolBufferSizeMB="50" diskExpiryThreadIntervalSeconds="120"memoryStoreEvictionPolicy="LFU" /><!-- 缓存授权信息 --> <cache name="expectauthorizationCache" maxEntriesLocalHeap="5000"eternal="false" timeToIdleSeconds="180" timeToLiveSeconds="600"overflowToDisk="true" statistics="true"></cache><!-- 缓存认证信息 --> <cache name="expectauthenticationCache" maxEntriesLocalHeap="5000"eternal="false" timeToIdleSeconds="180" timeToLiveSeconds="600"overflowToDisk="true" statistics="true"></cache><!-- shiro session的缓存 --><cache name="shiroSessionCache" maxEntriesLocalHeap="5000"eternal="false" timeToIdleSeconds="60" timeToLiveSeconds="60"overflowToDisk="true" statistics="true"></cache></ehcache>
如上配置,当用户登录后会 通过“缓存认证信息”对其认证进行缓存,只有在缓存时间到期后,缓存才会消失!!
如果在缓存到期之前,再次登录,那么shiro就不会去调用相关的业务认证AuthorizingRealm的实现,如:访问数据库的用户信息(是否是待审核状态等等)
- 那么,如题“立刻重新登录”,此时缓存的认证信息还为过期,也就不会去调用相关的业务认证AuthorizingRealm的实现, 也就是 虽然 用户信息修改了要等待审核,审核通过之后才能登陆,但是,由于缓存的存在,并且缓存并未到期,所以,实际上并不会走这部分业务验证。所以! 可以立刻重新登录(不必等待审核)。
- 解决 : 在修改用户信息时,手动清除认证的缓存信息,关键代码如下:
@Autowiredprivate EhCacheManager ehCacheManager;// 清除认证的缓存信息Doctor doctorLogin = (Doctor)SecurityUtils.getSubject().getPrincipal();Cache<String, ?> cache = ehCacheManager.getCache("doctorauthenticationCache");// 这里remove取的是登录的用户名Object obj = cache.remove(doctorLogin.getCellphone());return map;