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> ...
随机推荐
- CentOS下firewalld添加开放端口
添加 firewall-cmd --zone=public --add-port=/tcp --permanent (--permanent永久生效,没有此参数重启后失效) 重新载入 firewall ...
- [ASP.NET] 前台代码绑定后台变量方法总结 [转]
原文链接:https://www.cnblogs.com/lerit/archive/2010/10/22/1858007.html 经常会碰到在前台代码中要使用(或绑定)后台代码中变量值的问题.一般 ...
- numpy伪随机数的生成
numpy伪随机数的生成 normal函数 可以用normal来得到一个标准正态分布的4×4样本数组 >>> import numpy as np >>> samp ...
- SPOJ 703 SERVICE - Mobile Service 题解
题面 好题啊!~ 设f[i][j][k][l]表示已经处理完前i个请求后,a在j,b在k,c在l的最小值是多少: 那么f[i][p[i]][k][l]=min(f[i][p[i]][k][l],f[i ...
- 由对称性解2-SAT问题
由对称性解2-SAT问题 (by 伍昱,03年IOI国家集训队论文ppt) 2-SAT: 2-SAT就是2判定性问题,是一种特殊的逻辑判定问题. 2-SAT问题有何特殊性?该如何求解? 我们从一道例题 ...
- SQL注入的一些技巧分享
先上一道简单的ctf注入题: 一道利用order by进行注入的ctf题 很不错的一道利用order by的注入题,之前不知道order by除了爆字段还有这种操作. 原题地址:http://chal ...
- Coursera机器学习笔记(一) - 监督学习vs无监督学习
转载 http://daniellaah.github.io/2016/Machine-Learning-Andrew-Ng-My-Notes-Week-1-Introduction.html 一. ...
- ssl多人多附件多格式邮件发送
package com.dfmy.util; import java.io.File; import java.security.Security; import java.util.ArrayLis ...
- 关于Vue父子组件传值(复杂数据类型的值)的细节点
vue 父子组件传值是很常见的,多数情况下都是父传递给子的值是基础数据类型,如string,number,boolean, 当父组件值被修改时,子组件能够实时的作出改变. 如果父子传值的类型是复杂数据 ...
- 浅谈XXE
转自FReeBUF 0×00. 介绍 现在越来越多主要的web程序被发现和报告存在XXE(XML External Entity attack)漏洞,比如说facebook.paypal等等. 举个例 ...