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缓存的更多相关文章

  1. shiro框架学习-1-shiro基本概念

    1. 什么是权限控制 基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以访问而且只能访问自己被授权的资源, ...

  2. shiro框架学习-5-自定义Realm

    1. 自定义Realm基础 步骤: 创建一个类 ,继承AuthorizingRealm->AuthenticatingRealm->CachingRealm->Realm 重写授权方 ...

  3. shiro框架学习-6-Shiro内置的Filter过滤器及数据加解密

    1.  shiro的核心过滤器定义在枚举类DefaultFilter 中,一共有11个 ,配置哪个路径对应哪个拦截器进行处理 // // Source code recreated from a .c ...

  4. shiro基础学习(二)—shiro认证

    一.shiro简介      shiro是apache旗下一个开源框架,它将软件系统的安全认证相关的功能抽取出来,实现用户身份认证.权限授权.加密.会话管理等功能,组成了一个通用的安全认证框架. 以下 ...

  5. shiro基础学习(四)—shiro与项目整合

    一.认证 1.配置web.xml   2.配置applicationContext.xml      在applicationContext.xml中配置一个bean,ID和上面的过滤器的名称一致. ...

  6. shiro框架学习-9-shiroSession

    1.什么是会话session : 用户和程序直接的链接,程序可以根据session识别到哪个用户,和javaweb中的session类似 2. 什么是会话管理器SessionManager : 会话管 ...

  7. shiro框架学习-4- Shiro内置JdbcRealm

    1.  JdbcRealm 数据库准备 JdbcRealm就是用户的角色,权限都从数据库中读取,也就是用来进行用户认证授权的安全数据源更换为从数据库中读取,其他没有差别,首先在数据库创建三张表: CR ...

  8. shiro框架学习-3- Shiro内置realm

    1. shiro默认自带的realm和常见使用方法 realm作用:Shiro 从 Realm 获取安全数据 默认自带的realm:idae查看realm继承关系,有默认实现和自定义继承的realm ...

  9. shiro框架学习-2-springboot整合shiro及Shiro认证授权流程

    1. 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

随机推荐

  1. 一张图包含SEO一切要点

    看到一张好图 from http://www.rongyipiao.com/?p=8

  2. mariadb数据库增删改查

    1.常用数据类型 1)整数:int, bit 2)小数:decimal     #decimal(5,2)表示共有五位数,保留两位小数 3)字符串:varchar, char 4)日期时间:date, ...

  3. 查找两个有序数组中的第K个元素(find kth smallest element in 2 sorted arrays)

    查找两个有序数组中的第K个元素 int FindKth(int a[], int b[], int k, int astart, int aend, int bstart, int bend) { ; ...

  4. dos2unix Linux解决编写脚本出现“%0D

    ## Linux解决编写脚本出现“%0D”# 安装# yum install -y dos2unix# 然后进行转化一下脚本,将其中的install_mysql.sh换成你的脚本# dos2unix ...

  5. CodeSmith 找不到请求的 .Net Framework Data Provider

    连接数据库时候报标题的错误解决方案 安装mysql-connector-net-6.8.7.msi,下载地址:http://dev.mysql.com/downloads/connector/net/ ...

  6. C#各种委托介绍

    委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递.事件是一种特殊的委托. 一.委托的声明 Delegate Delegate 我们常用到的一种声明 Delegate 至少 ...

  7. 启用yarn的高可用

    选择高可用的主机,新的一台: 点运行结束后,会看到实例会多出一个备用的节点:

  8. Oracle 查看 impdp 正在执行的内容

    1. 今天进行数据库备份恢复 一直卡住  找了一下 公司另外一个部门的方神提供了一个方法连查看 具体在做什么操作: 2. 现象. impdp 到一个地方直接卡住不动 具体位置 view 这个地方足足卡 ...

  9. [转帖][Linux]systemd和sysV

    [Linux]systemd和sysV   转自:https://www.cnblogs.com/EasonJim/p/7168216.html 在Debian8中systemd和sysVinit同时 ...

  10. mongodb增删改查常用命令总结

    前言 去年我还折腾过mongodb,后来用不到也就没碰了,这就导致了我忘的一干二净,不得不感叹,编程这东西只要不用,就会忘没了.现在我想重拾mongodb,来总结一下常用命令,主要就是增删改查. 另外 ...