第十一章 缓存机制(二) Realm 缓存

Shiro 提供了CachingRealm,其实现了CacheManagerAware接口,提供了缓存的一些基础实现;另外AuthenticatingRealm及AuthorizingRealm分别提供了对AuthenticationInfo 和AuthorizationInfo信息的缓存。


ini配置

userRealm=com.github.zhangkaitao.shiro.chapter11.realm.UserRealm
userRealm.credentialsMatcher=$credentialsMatcher
userRealm.cachingEnabled=true
userRealm.authenticationCachingEnabled=true
userRealm.authenticationCacheName=authenticationCache
userRealm.authorizationCachingEnabled=true
userRealm.authorizationCacheName=authorizationCache
securityManager.realms=$userRealm
cacheManager=org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:shiro-ehcache.xml
securityManager.cacheManager=$cacheManager

userRealm.cachingEnabled:启用缓存,默认false;

userRealm.authenticationCachingEnabled:启用身份验证缓存,即缓存AuthenticationInfo 信息,默认false;

userRealm.authenticationCacheName:缓存AuthenticationInfo信息的缓存名称;

userRealm. authorizationCachingEnabled:启用授权缓存,即缓存AuthorizationInfo信息,默认false;

userRealm. authorizationCacheName:缓存AuthorizationInfo信息的缓存名称;

cacheManager:缓存管理器,此处使用EhCacheManager,即Ehcache实现,需要导入相应的Ehcache依赖,请参考pom.xml;


因为测试用例的关系,需要将Ehcache的CacheManager改为使用VM单例模式:

this.manager = new net.sf.ehcache.CacheManager(getCacheManagerConfigFileInputStream());

改为

this.manager = net.sf.ehcache.CacheManager.create(getCacheManagerConfigFileInputStream());


测试用例

@Test
public void testClearCachedAuthenticationInfo(){
	login(u1.getUsername(),password);
	userService.changePassword(u1.getId(), password+"1");
	
	RealmSecurityManager securityManager = (RealmSecurityManager) SecurityUtils.getSecurityManager();
	UserRealm userRealm = (UserRealm) securityManager.getRealms().iterator().next();
	userRealm.clearCachedAuthenticationInfo(SecurityUtils.getSubject().getPrincipals());
	
	login(u1.getUsername(),password+"1");
}

首先登录成功(此时会缓存相应的AuthenticationInfo),然后修改密码;此时密码就变了;

接着需要调用Realm的clearCachedAuthenticationInfo方法清空之前缓存的AuthenticationInfo;

否则下次登录时还会获取到修改密码之前的那个AuthenticationInfo;

@Test
public void testClearCachedAuthorizationInfo(){
	System.out.println("1");
	login(u1.getUsername(),password);
	System.out.println("2");
	SecurityUtils.getSubject().checkRole(r1.getRole());
	System.out.println("3");
	userService.correlationRoles(u1.getId(), r2.getId());
	System.out.println("4");
	
	RealmSecurityManager securityManager = (RealmSecurityManager) SecurityUtils.getSecurityManager();
	UserRealm userRealm = (UserRealm) securityManager.getRealms().iterator().next();
	userRealm.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
	System.out.println("5");
	
	SecurityUtils.getSubject().checkRole(r2.getRole());
}

和之前的用例差不多;此处调用Realm的clearCachedAuthorizationInfo清空之前缓存的AuthorizationInfo;

另外还有clearCache,其同时调用clearCachedAuthenticationInfo和clearCachedAuthorizationInfo,清空AuthenticationInfo和AuthorizationInfo。

UserRealm还提供了clearAllCachedAuthorizationInfo、clearAllCachedAuthenticationInfo、clearAllCache,用于清空整个缓存。


在某些清空下这种方式可能不是最好的选择,可以考虑直接废弃Shiro 的缓存,然后自己通过如AOP 机制实现自己的缓存;可以参考:

https://github.com/zhangkaitao/es/tree/master/web/src/main/java/com/sishuok/es/extra/aop


另外如果和Spring 集成时可以考虑直接使用Spring 的Cache 抽象,可以考虑使用SpringCacheManagerWrapper,其对Spring Cache进行了包装,转换为Shiro 的CacheManager实现:

https://github.com/zhangkaitao/es/blob/master/web/src/main/java/org/apache/shiro/cache/spring/SpringCacheManagerWrapper.java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值