shiro认证缓存信息导致:修改用户信息后立刻重新登录仍然能登录问题

问题描述

  • 使用shiro进行权限认证
  • 并且,使用了shiro的缓存管理,ehcache.xml配置如下
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的实现, 也就是 虽然 用户信息修改了要等待审核,审核通过之后才能登陆,但是,由于缓存的存在,并且缓存并未到期,所以,实际上并不会走这部分业务验证。所以! 可以立刻重新登录(不必等待审核)。
  • 解决 : 在修改用户信息时,手动清除认证的缓存信息,关键代码如下:
java 复制代码
			    @Autowired
			    private EhCacheManager ehCacheManager;

				// 清除认证的缓存信息
				Doctor doctorLogin = (Doctor)SecurityUtils.getSubject().getPrincipal();
				Cache<String, ?> cache = ehCacheManager.getCache("doctorauthenticationCache");
				// 这里remove取的是登录的用户名
				Object obj = cache.remove(doctorLogin.getCellphone());
				return map;
相关推荐
CodeAmaz2 小时前
SpringBoot两级缓存实现
spring boot·spring·缓存
小丁爱养花16 小时前
Redis 内部编码/单线程模型/string
数据库·redis·缓存·1024程序员节
爬山算法16 小时前
Redis(84)如何解决Redis的缓存击穿问题?
java·redis·缓存
Merlos_wind21 小时前
【Redis典型应用——缓存详解】
数据库·redis·缓存
缘友一世1 天前
LLama 3分组查询注意力与KV缓存机制
人工智能·深度学习·缓存·transformer·llama·gqa·kv缓存
阿祥~1 天前
windows 安装 Redis
数据库·redis·缓存
emma羊羊1 天前
【Redis】
数据库·redis·缓存
aristo_boyunv2 天前
Redis底层原理-持久化【详细易懂】
数据库·redis·缓存
羊锦磊2 天前
[ Redis ] SpringBoot集成使用Redis(补充)
java·数据库·spring boot·redis·spring·缓存·json
williamdsy2 天前
【清除 Mac DNS 缓存】Mac 电脑能访问外网却无法加载特定页面?你的 DNS 缓存“发霉”了!
macos·缓存