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;
相关推荐
十五年专注C++开发1 小时前
hiredis: 一个轻量级、高性能的 C 语言 Redis 客户端库
开发语言·数据库·c++·redis·缓存
张先shen3 小时前
亿级流量下的缓存架构设计:Redis+Caffeine多级缓存实战
数据库·redis·缓存
亚洲第一中锋_哈达迪8 小时前
详解缓存淘汰策略:LRU
后端·缓存·golang
vortex510 小时前
Redis渗透思路总结
数据库·redis·缓存
xinghunzhiye20101 天前
redis升级
数据库·redis·缓存
sqyaa.1 天前
Guava LoadingCache
jvm·缓存·guava
Hello.Reader1 天前
RedisJSON 内存占用剖析与调优
数据库·redis·缓存
千宇宙航1 天前
闲庭信步使用图像验证平台加速FPGA的开发:第九课——图像插值的FPGA实现
图像处理·计算机视觉·缓存·fpga开发
全栈凯哥1 天前
20.缓存问题与解决方案详解教程
java·spring boot·redis·后端·缓存
Hellyc2 天前
用户查询优惠券之缓存击穿
java·redis·缓存