在添加权限控制后,添加方法 查看

当用户访问”获取用户信息”、”新增用户”和”删除用户”的时,后台输出打印如下信息 ,

Druid数据源SQL监控

为了避免频繁访问数据库获取权限信息,在Shiro中加入缓存

缓存有基于Redis和Ehcache的

基于Redis

1.Shiro集成Redis的引入依赖

<dependency>
<groupId>org.crazycake</groupId>
<artifactId>shiro-redis</artifactId>
</dependency>

2.Redis配置

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.timeout=0

3.在ShiroConfig中配置Redis

public RedisManager redisManager() {
RedisManager redisManager = new RedisManager();
return redisManager;
} public RedisCacheManager cacheManager() {
RedisCacheManager redisCacheManager = new RedisCacheManager();
redisCacheManager.setRedisManager(redisManager());
return redisCacheManager;
}

在SecurityManager中加入RedisCacheManager

@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
……
securityManager.setCacheManager(cacheManager());
return securityManager;
}

4.测试

启动项目

访问访问”获取用户信息”、”新增用户”和”删除用户”,后台只打印一次获取权限信息

Druid数据源SQL监控

基于Ehcache

1.添加依赖

<!-- shiro ehcache -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.4.1</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>

2.添加配置

src\main\resources\config下添加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>

3.在ShiroConfig中

  注入Ehcache缓存

  添加getEhCacheManager()

@Bean
public EhCacheManager getEhCacheManager() {
EhCacheManager em = new EhCacheManager();
em.setCacheManagerConfigFile("classpath:config/shiro-ehcache.xml");
return em;
}

  将缓存对象注入到SecurityManager中

  修改securityManager()

@Bean
public SecurityManager securityManager() {
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
securityManager.setRealm(myShiroRealm());
securityManager.setRememberMeManager(rememberMeManager());
securityManager.setCacheManager(getEhCacheManager());
return securityManager;
}

4.测试

  启动项目,分别访问访问”获取用户信息”、”新增用户”,只获取一次权限信息

Spring boot后台搭建二为Shiro权限控制添加缓存的更多相关文章

  1. Spring boot后台搭建二集成Shiro权限控制

    上一篇文章,实现了用户验证 查看,接下来实现下权限控制 权限控制,是管理资源访问的过程,用于对用户进行的操作授权,证明该用户是否允许进行当前操作,如访问某个链接,某个资源文件等 Apache Shir ...

  2. Spring boot后台搭建二集成Shiro实现用户验证

    上一篇文章中介绍了Shiro 查看 将Shiro集成到spring boot的步骤: (1)定义一个ShiroConfig,配置SecurityManager Bean,SecurityManager ...

  3. Spring boot后台搭建一使用MyBatis集成Mapper和PageHelper

    目标: 使用 Spring  boot+MyBatis+mysql 集成 Mapper 和 PageHelper,实现基本的增删改查 先建一个基本的 Spring Boot 项目开启 Spring B ...

  4. Spring boot后台搭建二集成Shiro添加Remember Me

    上一片文章实现了用户验证  查看 当用户成功登录后,关闭浏览器,重新打开浏览器访问http://localhost:8080,页面会跳转到登录页,因为浏览器的关闭后之前的登录已失效 Shiro提供了R ...

  5. yii2搭建完美后台并实现rbac权限控制实例教程

    1.安装yii2 未安装的请参考yii2史上最简单式安装教程,没有之一 或者参考yii2实战教程之详细安装步骤 已安装的请继续看下一步操作 2.配置数据库 2.1 配置数据库 修改common/con ...

  6. shiro权限控制入门

    一:权限控制两种主要方式 粗粒度 URL 级别权限控制和细粒度方法级别权限控制 1.粗粒度 URL 级别权限控制 可以基于 Filter 实现在数据库中存放 用户.权限.访问 URL 对应关系, 当前 ...

  7. Spring Boot 最简单整合Shiro+JWT方式

    简介 目前RESTful大多都采用JWT来做授权校验,在Spring Boot 中可以采用Shiro和JWT来做简单的权限以及认证验证,在和Spring Boot集成的过程中碰到了不少坑.便结合自身以 ...

  8. Spring Boot+CXF搭建WebService(转)

    概述 最近项目用到在Spring boot下搭建WebService服务,对Java语言下的WebService了解甚少,而今抽个时间查阅资料整理下Spring Boot结合CXF打架WebServi ...

  9. 2017.2.13 开涛shiro教程-第十二章-与Spring集成(二)shiro权限注解

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第十二章-与Spring集成(二)shiro权限注解 shiro注 ...

随机推荐

  1. 摘:JAVA JXL API的详细使用

    转自:http://www.cr173.com/html/10377_1.html 1 开发调研1.1 需求描述MS的电子表格(Excel)是Office的重要成员,是保存统计数据的一种常用格式.作为 ...

  2. Python3和Python2 异常处理except的不同

    最近准备做个微信公众号的项目,但是微信平台的开发者文档介绍的是web.py,虽然有支持python3的版本.但是在介绍页面的还是python2的代码. python2.x的时候: try: raise ...

  3. springboot的第一节课

    快速开始spring boot应用 官方向导搭建boot应用 地址:http://start.spring.io/ 设置项目属性: 3.解压,拷贝到工作空间,导入maven项目 4.写Controll ...

  4. 【python】requests 异常处理

    以下是request.exceptions下的各种异常错误: RequestException: HTTPError(RequestException) UnrewindableBodyError(R ...

  5. 2019牛客暑期多校训练营(第三场)G: Removing Stones(启发式分治)

    题意:给定N,表示N堆石子,每堆石子数为a[],问多少个区间,可以满足“石子总和若为偶数,那么可以两两取来自不同堆的石子,直到取完: 如果为奇数,那么排除其中一个,然后可以两两取来自不同堆的石子,直到 ...

  6. postgres常用运维sql

    1.查看数据库大小 select pg_database_size('log_analysis'); postgres=# select pg_database_size('postExpress') ...

  7. tensorflow API _ 6 (tf.gfile)

    一.gfile模块是什么 tf.gfile模块的主要角色是:1.提供一个接近Python文件对象的API,以及2.提供基于TensorFlow C ++ FileSystem API的实现. C ++ ...

  8. “OKR播种机”JOHN DOERR–目标是对抗纷乱思绪的一针疫苗

    OKR培养出疯狂的想法,再加上对的人,奇迹就会出现 约翰·杜尔是美国最有影响力.最具创意.最不拘传统的冒险资本投资家之一.在短短10年内创造了高达1,000亿美元的经济价值.迄今为止,他已向 250家 ...

  9. Linux 系统管理——账号管理

    一.用户账号管理 1.用户账户概述 用户账户的常见分类: 超级用户:root  uid=0  gid=0  权限最大 普通用户:uid>=500  做一般权限的系统管理,权限有限. 程序用户:1 ...

  10. “知乎杯”2018 CCF 大学生计算机系统与程序设计竞赛 贪心算法(greedy)

    --> 贪心算法 1)题解 •        分别用V0.V1和V>=2表示度为0.1以及至少为2的顶点集合 •        对于每个顶点,维护三个属性: •        degree ...