shiro框架学习-8-shiro缓存
1. shiro进行认证授权时会查询数据库获取用户角色权限信息,每次登录都会去查询,这样对性能会又影响。可以设置缓存,查询时先去缓存中查找,缓存中没有再去数据库查询。


从shiro的架构图中可以看到有一个CacheManager——缓存管理器,可以使用 redis, hashmap, ehcache等作为缓存,可以在CacheManager中自定义。
shiro中提供了对认证信息和授权信息的缓存,默认是关闭认证信息缓存的,对于授权信息的缓存shiro默认开启的(因为授权的数据量大)。AuthenticatingRealm 及 AuthorizingRealm 分别提供了对AuthenticationInfo 和 AuthorizationInfo 信息的缓存。自定义的Realm继承了AuthorizingRealm, 最终会继承到CachingRealm:

查看 AuthenticatingRealm源码,可以看到它持有了CacheManager的一个 引用
public abstract class AuthenticatingRealm extends CachingRealm implements Initializable {
private static final Logger log = LoggerFactory.getLogger(AuthenticatingRealm.class);
private static final AtomicInteger INSTANCE_COUNT = new AtomicInteger();
private static final String DEFAULT_AUTHORIZATION_CACHE_SUFFIX = ".authenticationCache";
private CredentialsMatcher credentialsMatcher;
private Cache<Object, AuthenticationInfo> authenticationCache;
private boolean authenticationCachingEnabled;
private String authenticationCacheName;
private Class<? extends AuthenticationToken> authenticationTokenClass;
public AuthenticatingRealm() {
this((CacheManager)null, new SimpleCredentialsMatcher());
}
public AuthenticatingRealm(CacheManager cacheManager) {
this(cacheManager, new SimpleCredentialsMatcher());
}
public AuthenticatingRealm(CredentialsMatcher matcher) {
this((CacheManager)null, matcher);
}
public AuthenticatingRealm(CacheManager cacheManager, CredentialsMatcher matcher) {
this.authenticationTokenClass = UsernamePasswordToken.class;
// 认证的缓存默认是关闭的,因为登录次数不会很频繁
this.authenticationCachingEnabled = false;
int instanceNumber = INSTANCE_COUNT.getAndIncrement();
this.authenticationCacheName = this.getClass().getName() + ".authenticationCache";
if (instanceNumber > 0) {
this.authenticationCacheName = this.authenticationCacheName + "." + instanceNumber;
}
if (cacheManager != null) {
this.setCacheManager(cacheManager);
}
if (matcher != null) {
this.setCredentialsMatcher(matcher);
}
}
而CacheManager 仅仅是一个接口,它默认有三个实现类,而自定义shiro缓存,就是去继承 抽象类 AbstractCacheManager实现缓存管理。

来看下授权的类中的构造器:
public abstract class AuthorizingRealm extends AuthenticatingRealm implements Authorizer, Initializable, PermissionResolverAware, RolePermissionResolverAware {
private static final Logger log = LoggerFactory.getLogger(AuthorizingRealm.class);
private static final String DEFAULT_AUTHORIZATION_CACHE_SUFFIX = ".authorizationCache";
private static final AtomicInteger INSTANCE_COUNT = new AtomicInteger();
private boolean authorizationCachingEnabled;
private Cache<Object, AuthorizationInfo> authorizationCache;
private String authorizationCacheName;
private PermissionResolver permissionResolver;
private RolePermissionResolver permissionRoleResolver;
public AuthorizingRealm() {
this((CacheManager)null, (CredentialsMatcher)null);
}
public AuthorizingRealm(CacheManager cacheManager) {
this(cacheManager, (CredentialsMatcher)null);
}
public AuthorizingRealm(CredentialsMatcher matcher) {
this((CacheManager)null, matcher);
}
public AuthorizingRealm(CacheManager cacheManager, CredentialsMatcher matcher) {
if (cacheManager != null) {
this.setCacheManager(cacheManager);
}
if (matcher != null) {
this.setCredentialsMatcher(matcher);
}
// 授权默认开启了缓存
this.authorizationCachingEnabled = true;
this.permissionResolver = new WildcardPermissionResolver();
int instanceNumber = INSTANCE_COUNT.getAndIncrement();
this.authorizationCacheName = this.getClass().getName() + ".authorizationCache";
if (instanceNumber > 0) {
this.authorizationCacheName = this.authorizationCacheName + "." + instanceNumber;
}
}
shiro框架学习-8-shiro缓存的更多相关文章
- shiro框架学习-1-shiro基本概念
1. 什么是权限控制 基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以访问而且只能访问自己被授权的资源, ...
- shiro框架学习-5-自定义Realm
1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...
- shiro框架学习-6-Shiro内置的Filter过滤器及数据加解密
1. shiro的核心过滤器定义在枚举类DefaultFilter 中,一共有11个 ,配置哪个路径对应哪个拦截器进行处理 // // Source code recreated from a .c ...
- shiro基础学习(二)—shiro认证
一.shiro简介 shiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证.权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架. 以下 ...
- shiro基础学习(四)—shiro与项目整合
一.认证 1.配置web.xml 2.配置applicationContext.xml 在applicationContext.xml中配置一个bean,ID和上面的过滤器的名称一致. ...
- shiro框架学习-9-shiroSession
1.什么是会话session : 用户和程序直接的链接,程序可以根据session识别到哪个用户,和javaweb中的session类似 2. 什么是会话管理器SessionManager : 会话管 ...
- shiro框架学习-4- Shiro内置JdbcRealm
1. JdbcRealm 数据库准备 JdbcRealm就是用户的角色,权限都从数据库中读取,也就是用来进行用户认证授权的安全数据源更换为从数据库中读取,其他没有差别,首先在数据库创建三张表: CR ...
- shiro框架学习-3- Shiro内置realm
1. shiro默认自带的realm和常见使用方法 realm作用:Shiro 从 Realm 获取安全数据 默认自带的realm:idae查看realm继承关系,有默认实现和自定义继承的realm ...
- shiro框架学习-2-springboot整合shiro及Shiro认证授权流程
1. 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...
随机推荐
- unity shader 波动圈
c# //////////////////////////////////////////// // CameraFilterPack - by VETASOFT 2018 ///// /////// ...
- vmnet2访问外网
1.vmnet2用于内网之间的访问,外部网络访问不了它.它可以访问外网,要想访问外网就必须有真实主机共享网络给它 2.[root@localhost ~]# vim /etc/sysconfig/ne ...
- 【CUDA开发】CUDA的安装、Nvidia显卡型号及测试
说明:想要让Theano在Windows8.1下能利用GPU并行运算,必须有支持GPU并行运算的Nvidia显卡,且要安装CUDA,千万不要电脑上是Intel或AMD的显卡,却要编写CUDA. 文中用 ...
- 深入理解java:4.1. 框架编程之Spring MVC
说到java的mvc框架,struts2和springmvc想必大家都知道, Spring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高 ...
- for (;;) 与 while (true),哪个更快?
Java技术栈 www.javastack.cn 优秀的Java技术公众号 在 JDK8u 的 jdk 项目下做个很粗略的搜索: mymbp:/Users/me/workspace/jdk8u/jdk ...
- Comet OJ - Contest #13
Rank53. 第一次打这种比赛.还是有不少问题的,以后改吧. A题WA了两次罚了不少时. C写到一半发现只能过1,就先弃了. D一眼没看出来.第二眼看出来就是一个类似于复数的快速幂. 然后B切了. ...
- Python常用库整理
Python常用库整理 Python中到底有哪些库会让程序员爱不释手?以至于一次上瘾,造成永久性伤害(这句话好像在哪里见过),今天我们就来整理一番这样的库,欢迎各位在评论区或者私信我添加或者修改相关库 ...
- 22、nlpir 人工智能
练习介绍 [程序功能] 我们将完成一个和语义识别相关的爬虫程序,输入任意词汇.句子.文章或段落,会返回联想的词汇. [背景信息] 有一个非常牛的处理语言的网站nlpir,上面有非常多的处理语言的功能( ...
- spring-cloud学习BUG
1.com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: c ...
- 2019-11-29-dotnet-使用-System.CommandLine-写命令行程序
title author date CreateTime categories dotnet 使用 System.CommandLine 写命令行程序 lindexi 2019-11-29 08:33 ...