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;
相关推荐
啦啦啦_999910 分钟前
Redis-0-业务逻辑
数据库·redis·缓存
自不量力的A同学41 分钟前
Redisson 4.2.0 发布,官方推荐的 Redis 客户端
数据库·redis·缓存
fengxin_rou1 小时前
[Redis从零到精通|第四篇]:缓存穿透、雪崩、击穿
java·redis·缓存·mybatis·idea·多线程
fengxin_rou1 小时前
黑马点评实战篇|第二篇:商户查询缓存
缓存
笨蛋不要掉眼泪2 小时前
Redis哨兵机制全解析:原理、配置与实战故障转移演示
java·数据库·redis·缓存·bootstrap
ALex_zry14 小时前
Redis Cluster 分布式缓存架构设计与实践
redis·分布式·缓存
shuair19 小时前
redis缓存预热、缓存击穿、缓存穿透、缓存雪崩
redis·spring·缓存
上架ipa20 小时前
redis图形化客户端功能对比
redis·缓存
历程里程碑1 天前
21:重谈重定义理解一切皆“文件“及缓存区
linux·c语言·开发语言·数据结构·c++·算法·缓存
fengxin_rou1 天前
[Redis从零到精通|第三篇]:缓存更新指南
java·数据库·redis·spring·缓存