前言

在 shiro 中每次去拦截请求进行权限认证的时候,都会去数据库查询该用户的所有权限信息, 这个时候就是有一个问题了,因为用户的权限信息在短时间内是不可变的,每次查询出来的数据其实都是重复数据,没必要每次都去重新获取这个数据,统一放在缓存中进行管理,这个时候,我们只需要获取一次权限信息,存入到缓存中,待缓存过期后,再次重新获取即可。

例如,我执行一个查询多次,它执行多次权限查询。

使用 Reids 缓存

加入 shiro-redis 依赖

<!-- shiro-redis -->
<dependency>
<groupId>org.crazycake</groupId>
<artifactId>shiro-redis</artifactId>
<version>2.4.2.1-RELEASE</version>
</dependency>

配置 Redis

和前面系统配置 Reids 一样。

1. 在系统配置文件中加入 Redis 配置参数:

spring:
redis:
host: 192.168.19.200 # host ,默认 localhost
port: 6379 # 端口号,默认6379
pool:
# 设置都是默认值,可以按需求设计
max-active: 8 # 可用连接实例的最大数目,默认值为8;如果赋值为-1,则表示不限制;
max-idle: 8 # 控制一个pool最多有多少个状态为idle(空闲的)的redis实例,默认值也是8。
max-wait: -1 # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。
min-idle: 0 # 控制一个pool最少有多少个状态为idle(空闲的)的redis实例,默认值为0。
timeout: 0 # 连接超时时间 单位 ms,默认为0
password: master # 密码,根据自己的 redis 设计,默认为空
  1. 这个我们是要使用 RedisManager 管理我们的 Redis,它默认没有注入我们设置的这些参数,需要我们自己手动创建一个注入我们设置的参数。
@ConfigurationProperties(prefix = "spring.redis")
public class CustomRedisManager extends RedisManager { }

配置 Shiro 缓存

    /**
* redis 管理
*/
@Bean
public CustomRedisManager customRedisManager() {
return new CustomRedisManager();
} /**
* redis 缓存
*/
@Bean
public RedisCacheManager cacheManager(CustomRedisManager redisManager) {
RedisCacheManager redisCacheManager = new RedisCacheManager();
redisCacheManager.setRedisManager(redisManager);
return redisCacheManager;
} @Bean
public SecurityManager securityManager(OAuth2Realm oAuth2Realm, SessionManager sessionManager, RedisCacheManager cacheManager) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
// 可以添加多个认证,执行顺序是有影响的
securityManager.setRealm(oAuth2Realm);
securityManager.setSessionManager(sessionManager);
// 注册 缓存
securityManager.setCacheManager(cacheManager);
return securityManager;
}

使用测试

我们加入缓存后,看是个什么情况:

执行多次请求,只执行了一次查询权限的 SQL。

可以去 redis-cli 查看 keys,检查是否存在权限对象的 key。

使用 Ehcache 缓存

加入依赖

<!-- shiro ehcache -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.3.2</version>
</dependency>
<!-- ehchache -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>

Ehcache 配置

新增一个 Ehcache 配置文件 shiro-ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir/Tmp_EhCache" />
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" /> <!-- 登录记录缓存锁定1小时 -->
<cache
name="passwordRetryCache"
maxEntriesLocalHeap="2000"
eternal="false"
timeToIdleSeconds="3600"
timeToLiveSeconds="0"
overflowToDisk="false"
statistics="true" />
</ehcache>

配置 EhCache 缓存

    /**
* EhCache 缓存
*/
@Bean
public EhCacheManager ehCacheManager() {
EhCacheManager em = new EhCacheManager();
em.setCacheManagerConfigFile("classpath:config/shiro-ehcache.xml");
return em;
} @Bean
public SecurityManager securityManager(OAuth2Realm oAuth2Realm, SessionManager sessionManager,
RedisCacheManager cacheManager, EhCacheManager ehCacheManager) {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
// 可以添加多个认证,执行顺序是有影响的
securityManager.setRealm(oAuth2Realm);
securityManager.setSessionManager(sessionManager);
// 设置缓存
securityManager.setCacheManager(ehCacheManager);
return securityManager;
}

参考文章

学习Spring Boot:(十九)Shiro 中使用缓存的更多相关文章

  1. spring Boot(十九):使用Spring Boot Actuator监控应用

    spring Boot(十九):使用Spring Boot Actuator监控应用 微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台 ...

  2. spring boot(十四)shiro登录认证与权限管理

    这篇文章我们来学习如何使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在Java领域一般有Spring Security ...

  3. (转)Spring Boot (十九):使用 Spring Boot Actuator 监控应用

    http://www.ityouknow.com/springboot/2018/02/06/spring-boot-actuator.html 微服务的特点决定了功能模块的部署是分布式的,大部分功能 ...

  4. 学习Spring Boot:(二十六)使用 RabbitMQ 消息队列

    前言 前面学习了 RabbitMQ 基础,现在主要记录下学习 Spring Boot 整合 RabbitMQ ,调用它的 API ,以及中间使用的相关功能的记录. 相关的可以去我的博客/RabbitM ...

  5. 学习Spring Boot:(二十五)使用 Redis 实现数据缓存

    前言 由于 Ehcache 存在于单个 java 程序的进程中,无法满足多个程序分布式的情况,需要将多个服务器的缓存集中起来进行管理,需要一个缓存的寄存器,这里使用的是 Redis. 正文 当应用程序 ...

  6. 学习 Spring Boot 知识看这一篇就够了

    从2016年因为工作原因开始研究 Spring Boot ,先后写了很多关于 Spring Boot 的文章,发表在技术社区.我的博客和我的公号内.粗略的统计了一下总共的文章加起来大概有六十多篇了,其 ...

  7. (转)Spring Boot (十四): Spring Boot 整合 Shiro-登录认证和权限管理

    http://www.ityouknow.com/springboot/2017/06/26/spring-boot-shiro.html 这篇文章我们来学习如何使用 Spring Boot 集成 A ...

  8. Spring Boot(十四):spring boot整合shiro-登录认证和权限管理

    Spring Boot(十四):spring boot整合shiro-登录认证和权限管理 使用Spring Boot集成Apache Shiro.安全应该是互联网公司的一道生命线,几乎任何的公司都会涉 ...

  9. Spring Boot (十四): Spring Boot 整合 Shiro-登录认证和权限管理

    这篇文章我们来学习如何使用 Spring Boot 集成 Apache Shiro .安全应该是互联网公司的一道生命线,几乎任何的公司都会涉及到这方面的需求.在 Java 领域一般有 Spring S ...

随机推荐

  1. 第7章 使用寄存器点亮LED灯

    第7章     使用寄存器点亮LED灯 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...

  2. SS、SP、BP寄存器

    SS, SP, BP 三个寄存器 SS:存放栈的段地址: SP:堆栈寄存器SP(stack pointer)存放栈的偏移地址; BP: 基数指针寄存器BP(base pointer)是一个寄存器,它的 ...

  3. STS-创建spring配置文件

    1.创建一个bean文件 2.输入文件名applicationContext.xml 3.这里会自动显示模板文件 4.创建后,自动填充头不定义 到这里就可以发现,我们创建spring文件时,需要的配置 ...

  4. VBA 连接,提醒 rs AS new adodb.recordset 的变量未定义

    解决方法: 菜单-工程-引用Microsoft ActiveX Data Objects 2.x Library 定位……msado15.dll

  5. 在 R 中估计 GARCH 参数存在的问题(续)

    目录 在 R 中估计 GARCH 参数存在的问题(续) rugarch 包的使用 简单实验 rugarch 参数估计的行为 极端大样本 结论 在 R 中估计 GARCH 参数存在的问题(续) 本文承接 ...

  6. 20155226《网络攻防》 Exp5 MSF基础应用

    20155226<网络攻防> Exp5 MSF基础应用 基础问题回答 1.用自己的话解释什么是exploit,payload,encode? exploit : Exploit的英文意思就 ...

  7. SpingMVC的<context:component-scan>包扫描踩坑记录

        公司项目配置的Spring项目的包扫描有点问题,出现了一个被Spring容器管理的Bean被创建了2次的现象.在此记录下解决的过程,方便后续查阅. 改动前: 容器启动监听器中会扫描全部包,创建 ...

  8. C语言学习之结构体

    前言 一直以来,C语言的学习都在入门阶段,只用到数组.函数.循环.选择.位运算这些基本的知识,较少用到指针.预处理.结构体.枚举类型.文件操作等这些C语言的精髓内容,现在想想真不敢说自己熟练掌握C语言 ...

  9. maven核心,pom.xml详解

    什么是pom?    pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的u ...

  10. flask之jinjia2模板(二)

    1.1.模板传参 (1)主程序 from flask import Flask,render_template app = Flask(__name__) @app.route('/') def he ...