目录贴:跟我学Shiro目录贴

Shiro提供了类似于Spring的Cache抽象,即Shiro本身不实现Cache,但是对Cache进行了又抽象,方便更换不同的底层Cache实现。对于Cache的一些概念可以参考我的《Spring Cache抽象详解》:http://jinnianshilongnian.iteye.com/blog/2001040

Shiro提供的Cache接口:

  1. public interface Cache<K, V> {
  2. //根据Key获取缓存中的值
  3. public V get(K key) throws CacheException;
  4. //往缓存中放入key-value,返回缓存中之前的值
  5. public V put(K key, V value) throws CacheException;
  6. //移除缓存中key对应的值,返回该值
  7. public V remove(K key) throws CacheException;
  8. //清空整个缓存
  9. public void clear() throws CacheException;
  10. //返回缓存大小
  11. public int size();
  12. //获取缓存中所有的key
  13. public Set<K> keys();
  14. //获取缓存中所有的value
  15. public Collection<V> values();
  16. }

Shiro提供的CacheManager接口:

  1. public interface CacheManager {
  2. //根据缓存名字获取一个Cache
  3. public <K, V> Cache<K, V> getCache(String name) throws CacheException;
  4. }

Shiro还提供了CacheManagerAware用于注入CacheManager

  1. public interface CacheManagerAware {
  2. //注入CacheManager
  3. void setCacheManager(CacheManager cacheManager);
  4. }

Shiro内部相应的组件(DefaultSecurityManager)会自动检测相应的对象(如Realm)是否实现了CacheManagerAware并自动注入相应的CacheManager。

本章用例使用了与第六章的代码。

Realm缓存

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

ini配置

  1. userRealm=com.github.zhangkaitao.shiro.chapter11.realm.UserRealm
  2. userRealm.credentialsMatcher=$credentialsMatcher
  3. userRealm.cachingEnabled=true
  4. userRealm.authenticationCachingEnabled=true
  5. userRealm.authenticationCacheName=authenticationCache
  6. userRealm.authorizationCachingEnabled=true
  7. userRealm.authorizationCacheName=authorizationCache
  8. securityManager.realms=$userRealm
  9. cacheManager=org.apache.shiro.cache.ehcache.EhCacheManager
  10. cacheManager.cacheManagerConfigFile=classpath:shiro-ehcache.xml
  11. 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());

测试用例

  1. @Test
  2. public void testClearCachedAuthenticationInfo() {
  3. login(u1.getUsername(), password);
  4. userService.changePassword(u1.getId(), password + "1");
  5. RealmSecurityManager securityManager =
  6. (RealmSecurityManager) SecurityUtils.getSecurityManager();
  7. UserRealm userRealm = (UserRealm) securityManager.getRealms().iterator().next();
  8. userRealm.clearCachedAuthenticationInfo(subject().getPrincipals());
  9. login(u1.getUsername(), password + "1");
  10. }

首先登录成功(此时会缓存相应的AuthenticationInfo),然后修改密码;此时密码就变了;接着需要调用Realm的clearCachedAuthenticationInfo方法清空之前缓存的AuthenticationInfo;否则下次登录时还会获取到修改密码之前的那个AuthenticationInfo;

  1. @Test
  2. public void testClearCachedAuthorizationInfo() {
  3. login(u1.getUsername(), password);
  4. subject().checkRole(r1.getRole());
  5. userService.correlationRoles(u1.getId(), r2.getId());
  6. RealmSecurityManager securityManager =
  7. (RealmSecurityManager) SecurityUtils.getSecurityManager();
  8. UserRealm userRealm = (UserRealm)securityManager.getRealms().iterator().next();
  9. userRealm.clearCachedAuthorizationInfo(subject().getPrincipals());
  10. subject().checkRole(r2.getRole());
  11. }

和之前的用例差不多;此处调用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

Session缓存

当我们设置了SecurityManager的CacheManager时,如:

  1. securityManager.cacheManager=$cacheManager

当我们设置SessionManager时:

  1. sessionManager=org.apache.shiro.session.mgt.DefaultSessionManager
  2. securityManager.sessionManager=$sessionManager

如securityManager实现了SessionsSecurityManager,其会自动判断SessionManager是否实现了CacheManagerAware接口,如果实现了会把CacheManager设置给它。然后sessionManager会判断相应的sessionDAO(如继承自CachingSessionDAO)是否实现了CacheManagerAware,如果实现了会把CacheManager设置给它;如第九章的MySessionDAO就是带缓存的SessionDAO;其会先查缓存,如果找不到才查数据库。

对于CachingSessionDAO,可以通过如下配置设置缓存的名称:

  1. sessionDAO=com.github.zhangkaitao.shiro.chapter11.session.dao.MySessionDAO
  2. sessionDAO.activeSessionsCacheName=shiro-activeSessionCache

activeSessionsCacheName默认就是shiro-activeSessionCache。

示例源代码:https://github.com/zhangkaitao/shiro-example;可加群 231889722 探讨Spring/Shiro技术。

第十一章 缓存机制——《跟我学Shiro》的更多相关文章

  1. (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...

  2. (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...

  3. 第十四章 SSL——《跟我学Shiro》

    目录贴:跟我学Shiro目录贴 对于SSL的支持,Shiro只是判断当前url是否需要SSL登录,如果需要自动重定向到https进行访问. 首先生成数字证书,生成证书到D:\localhost.key ...

  4. 第三章 授权——《跟我学Shiro》

    转发地址:https://www.iteye.com/blog/jinnianshilongnian-2020017 目录贴:跟我学Shiro目录贴 授权,也叫访问控制,即在应用中控制谁能访问哪些资源 ...

  5. 第十三章 RememberMe——《跟我学Shiro》

    转发地址:https://www.iteye.com/blog/jinnianshilongnian-2031823 目录贴:跟我学Shiro目录贴 Shiro提供了记住我(RememberMe)的功 ...

  6. Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】

    转自:https://blog.csdn.net/linxingliang/article/details/52263763 spring boot 自学笔记(三) Redis集成—RedisTemp ...

  7. 第二十三章 多项目集中权限管理及分布式会话——《跟我学Shiro》

    二十三章 多项目集中权限管理及分布式会话——<跟我学Shiro> 博客分类: 跟我学Shiro 跟我学Shiro  目录贴:跟我学Shiro目录贴 在做一些企业内部项目时或一些互联网后台时 ...

  8. 《跟我学shiro》

    张开涛<跟我学shiro>博客系列: Shiro目录 第一章  Shiro简介 第二章  身份验证 第三章  授权 第四章  INI配置 第五章  编码/加密 第六章  Realm及相关对 ...

  9. 跟我学Shiro目录贴

    转发地址:https://www.iteye.com/blog/jinnianshilongnian-2018398 扫一扫,关注我的公众号 购买地址 历经三个月左右时间,<跟我学Shiro&g ...

随机推荐

  1. django安装tinymce

    1. pip install django-tinymce 2. 运行:python manage.py collectstatic 3. 编辑 settings.py 4. TINYMCE_JS_U ...

  2. BZOJ 1181: [CROATIAN2009] IZBROI选举(二分+dp)

    题面 在一个地区的选举中,共有V个人参加了投票,每一票只可能投给N个政党中的一个.当地的议会共有M个席位.不妨将N个政党编号为1到N,并且设编号为i的政党最终的得票为Vi,则议会中的席位按如下规则分配 ...

  3. NOIP2018 保卫王国(动态DP)

    题意 求最小权值点覆盖. mmm次询问,每次给出两个点,分别要求每个点必须选或必须不选,输出每次的最小权值覆盖或者无解输出−1-1−1 题解 强制选或者不选可以看做修改权值为±∞\pm\infin±∞ ...

  4. 微信小程序学习记录(一)

    如何定义一个全局变量: 1,在根目录下app.js中添加 App({ globalData: { g_isPlayingMusic : false, g_currentMusicPostId :nul ...

  5. mongodb存储引擎WiredTiger

      MongoDB3.2后默认采用WiredTiger存储引擎. 组成 WiredTiger由三部分组成: Mongos: 负责查询请求的路由和对ShardServer的管理: ConfigServe ...

  6. msyql的子查询,或者叫嵌套查询

    INNER和OUTER可以省略

  7. Linux操作系统常用命令合集——第六篇-软件包操作(2个命令)

    一.前言介绍 软件包即程序包 程序包管理 关键词:rpm程序包管理.YUM仓库管理.源码编译安装 程序包管理: 将编译好的应用程序的各组成文件打包一个或几个程序包文件,从而方便快捷地实现程序包的安装. ...

  8. Gym - 101981E 思维

    Gym - 101981EEva and Euro coins 题意:给你两个长度皆为n的01串s和t,能做的操作是把连续k个相同的字符反转过来,问s串能不能变成t串. 一开始把相同的漏看了,便以为是 ...

  9. Docker 安装ELK之 zz

    Docker 安装ELK之(2)   加新 2018-10-24 16:23:08 浏览2006 docker LOG js Image service   先安装Docker [root@jiaxi ...

  10. 幽默的讲解六种Socket I/O模型

    很幽默的讲解六种Socket I/O模型 本文简单介绍了当前Windows支持的各种Socket I/O模型,如果你发现其中存在什么错误请务必赐教. 一:select模型 二:WSAAsyncSele ...