Shiro + Redis集成思路
首先,确保Spring配置完毕了。
集成Shiro
1、在pom.xml中追加依赖
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.2</version>
</dependency>
2、追加spring-shiro.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="myShiroRealm" class="io.spldeolin.bestpractice.shiro.component.Realm">
<property name="cacheManager" ref="cacheManager" />
</bean> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="myShiroRealm" />
<property name="cacheManager" ref="cacheManager" />
<property name="sessionManager" ref="sessionManager" />
</bean> <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionIdUrlRewritingEnabled" value="false" />
<property name="globalSessionTimeout" value="3600000" />
</bean> <!-- 自带的、缓存在内存的、不支持集群的缓存管理器 -->
<bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" /> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager" />
<!-- loginUrl 未认证时,访问需要认证资源时的重定向url -->
<property name="loginUrl" value="/" />
<!-- successUrl 登录成功后的重定向url -->
<property name="successUrl" value="/loginsuccess.jhtml" />
<!-- unauthorizedUrl 访问无权限资源时的重定向url -->
<property name="unauthorizedUrl" value="/error.jhtml" />
<property name="filterChainDefinitions">
<value>
/** = anon
</value>
</property>
</bean> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
depends-on="lifecycleBeanPostProcessor">
<property name="proxyTargetClass" value="true" />
</bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean> </beans>
有三个主要的组件,Realm、SecurityManager、ShiroFilter。
Realm代表用来取得用于验证和授权的数据的策略。
SecurityManager持有Realm对象、CacheManager对象、SessionManager对象。后两者代表缓存策略和会话管理策略,演示代码采取的是Shiro默认策略
ShiroFilter代表过滤器,为web.xml中配置的过滤器提供支持。
3、在web.xml中追加ShiroFilter
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
4、实现spring-shiro.xml涉及到的自定义类,示例代码中,只需要实现Realm
集成Redis
5、在pom.xml中追加依赖
<dependency>
<groupId>org.crazycake</groupId>
<artifactId>shiro-redis</artifactId>
<version>2.4.2.1-RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>
6、追加spring-redis.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="redisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxActive}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="maxWaitMillis" value="${redis.pool.maxWait}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
</bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"
destroy-method="destroy">
<constructor-arg ref="redisPoolConfig" />
<constructor-arg value="${redis.host}" />
<constructor-arg type="int" value="${redis.port}" />
<constructor-arg type="int" value="${redis.timeout}" />
<constructor-arg type="java.lang.String" value="${redis.password}" />
<constructor-arg type="int" value="${redis.dbindex}" />
</bean> <bean id="redisClient" class="io.spldeolin.logindemo.shiro.component.RedisClient4Shiro">
<constructor-arg name="jedisPool" ref="jedisPool" />
<property name="expire" value="${redis.default.expire}" />
</bean> </beans>
实际上,还需要追加redis.properties,这里省略
7、修改spring-shiro.xml中SecurityManager的SessionManager(会话管理策略)
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="realm" />
<property name="cacheManager" ref="cacheManager" />
<!-- 【securityManager的sessionManager相关】 -->
<property name="sessionManager">
<!-- Shiro自带的session管理方式——DefaultWebSessionManager和MemorySessionDAO -->
<!-- <bean class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionIdUrlRewritingEnabled" value="false" /> <property
name="globalSessionTimeout" value="3600000" /> </bean> -->
<!-- Redis的session管理方式——DefaultWebSessionManager和RedisSessionDAO -->
<bean
class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="globalSessionTimeout" value="1800000" />
<property name="sessionValidationInterval"
value="1800000" />
<property name="sessionDAO">
<bean class="org.crazycake.shiro.RedisSessionDAO">
<property name="redisManager" ref="redisClient" />
</bean>
</property>
<property name="sessionIdCookie">
<bean class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg name="name"
value="custom.session" />
<property name="path" value="/" />
</bean>
</property>
<property name="sessionIdUrlRewritingEnabled"
value="false" />
</bean>
</property>
<!-- 记住我的失效时间,单位是秒 -->
<property name="rememberMeManager.cookie.maxAge" value="100000"></property>
</bean>
这里的关键是org.crazycake.shiro.RedisSessionDAO
8、修改spring-shiro.xml中SecurityManager的cacheManager(缓存策略)
<!-- Shiro自带的缓存管理 -->
<!-- <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"
/> -->
<!-- Redis的缓存管理 -->
<bean id="cacheManager" class="org.crazycake.shiro.RedisCacheManager">
<property name="keyPrefix" value="shiro_redis_session:" />
<property name="redisManager" ref="redisClient" />
</bean>
可以看到spring-redis.xml与新的spring-shiro.xml的联系点在redisClient这个bean。
9、实现RedisClient4Shiro类
public class RedisClient4Shiro extends RedisManager { private JedisPool jedisPool = null; public RedisClient4Shiro(JedisPool jedisPool) {
this.jedisPool = jedisPool;
} @Override
public void init() {
super.init();
} @Override
public byte[] get(byte[] key) {
Jedis jedis = jedisPool.getResource();
byte[] value;
try {
value = jedis.get(key);
} catch (Exception e) {
throw new RuntimeException("redis operation error:", e);
} finally {
jedis.close();
}
return value;
} @Override
public byte[] set(byte[] key, byte[] value) {
Jedis jedis = jedisPool.getResource();
try {
jedis.set(key, value);
Integer expire = getExpire();
if (expire != 0) {
jedis.expire(key, expire);
}
} catch (Exception e) {
throw new RuntimeException("redis operation error:", e);
} finally {
jedis.close();
}
return value;
} @Override
public byte[] set(byte[] key, byte[] value, int expire) {
Jedis jedis = jedisPool.getResource();
try {
jedis.set(key, value);
if (expire != 0) {
jedis.expire(key, expire);
}
} catch (Exception e) {
throw new RuntimeException("redis operation error:", e);
} finally {
jedis.close();
} return value;
} @Override
public void del(byte[] key) {
Jedis jedis = jedisPool.getResource();
try {
jedis.del(key);
} catch (Exception e) {
throw new RuntimeException("redis operation error:", e);
} finally {
jedis.close();
}
} @Override
public void flushDB() {
Jedis jedis = jedisPool.getResource();
try {
jedis.flushDB();
} catch (Exception e) {
throw new RuntimeException("redis operation error:", e);
} finally {
jedis.close();
}
} @Override
public Long dbSize() {
Long dbSize = Long.valueOf(0L);
Jedis jedis = jedisPool.getResource(); try {
dbSize = jedis.dbSize();
} catch (Exception e) {
throw new RuntimeException("redis operation error:", e);
} finally {
jedis.close();
}
return dbSize;
} @Override
public Set<byte[]> keys(String pattern) {
Set<byte[]> keys = null;
Jedis jedis = jedisPool.getResource(); try {
keys = jedis.keys(pattern.getBytes());
} catch (Exception e) {
throw new RuntimeException("redis operation error:", e);
} finally {
jedis.close();
}
return keys;
} }
详细:Github
Shiro + Redis集成思路的更多相关文章
- shiro和redis集成,前后端分离
前言 框架:springboot+shiro+redis+vue 最近写前后端分离授权的对账平台系统,采取了shiro框架,若采用shiro默认的cookie进行授权验证时,一直存在由于跨域造成前端请 ...
- Redis入门指南(第2版) Redis设计思路学习与总结
https://www.qcloud.com/community/article/222 宋增宽,腾讯工程师,16年毕业加入腾讯,从事海量服务后台设计与研发工作,现在负责QQ群后台等项目,喜欢研究技术 ...
- Redis设计思路学习与总结
版权声明:本文由宋增宽原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/222 来源:腾云阁 https://www.qclo ...
- Shiro+Redis实现tomcat集群session共享
一.背景 当我们使用了nginx做项目集群以后,就会出现一个很严重的问题亟待解决,那就是:tomcat集群之间如何实现session共享的问题,如果这个问题不解决,就会出现登陆过后再次请求资源依旧 ...
- Springboot2.x+shiro+redis(Lettuce)整合填坑
主要记录关键和有坑的地方 前提: 1.SpringBoot+shiro已经集成完毕,如果没有集成,先查阅之前的Springboot2.0 集成shiro权限管理 2.redis已经安装完成 3.red ...
- springboot+shiro+redis项目整合
介绍: Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.使用Shiro的易于理解的API,您可以快速.轻松地获得任何应用程序,从最小的移动应用程序到最 ...
- AngularJS进阶(二十七)实现二维码信息的集成思路
AngularJS实现二维码信息的集成思路 赠人玫瑰,手留余香.若您感觉此篇博文对您有用,请花费2秒时间点个赞,您的鼓励是我不断前进的动力,与君共勉! 注:点击此处进行知识充电 ...
- Spring Boot(十一)Redis集成从Docker安装到分布式Session共享
一.简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API,Redis也是技术领域使用最为广泛的存储中间件,它是 ...
- spring-boot+mybatisPlus+shiro的集成demo 我用了5天
spring-boot + mybatis-plus + shiro 的集成demo我用了五天 关于shiro框架,我还是从飞机哪里听来的,就连小贱都知道,可我母鸡啊.简单百度了下,结论很好上手,比s ...
随机推荐
- 【转载】Spring JdbcTemplate详解
JdbcTemplate简介 Spring对数据库的操作在jdbc上面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中. JdbcTempla ...
- ORA-00001:unique constraint violated解决
转自:https://www.2cto.com/database/201211/172340.html ORA-00001:unique constraint violated解决 今天往数据库中插入 ...
- 如何使用.gitignore文件删除掉已经提交的文件
如何使用.gitignore文件删除掉已经提交的文件 2018.06.06 22:13:38字数 96阅读 116 如果你的文件已经提交,而此时你才发现忘了添加.gitignore文件,不用担心, ...
- Robo 3T SQL
查询指定日期,指定显示字段,排序,注释功能 db.getCollection('spuBasisInfo') .find({"createTime":{$gte:ISODate(& ...
- [#Linux] CentOS 7 美化调优
优化美化系统,是为了让新系统能更顺眼顺手,符合自己过去在windows下的使用习惯,从而实现平稳过渡. 正如开篇时谈到的,现在的桌面版linux已相当友好(特别是Ubuntu),基本不需要做什么额外设 ...
- Computer Vision_33_SIFT:LIFT: Learned Invariant Feature Transform——2016
此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...
- Find The Multiple (DFS递归)
题意:输入一个不超过200的数 n,然后求得一个数字k,数字满足:能被n整除,每一位只有0,1.这样的数字k会有很多个,然以输出一个就可以. 注意unsigned __int64的范围,-(10^19 ...
- 前端笔记-bom
BOM对象 BOM即浏览器对象模型,它与dom不同的是可以操作浏览器窗口,使用它的接口我们可以改变窗口,状态栏,文本,及其他与除页面以外其他动作,使得js可以和我们浏览器进行沟通 窗口 即window ...
- Factorization Machines
- kafka读书笔记《kafka并不难学》
======第一章 1 在高并发场景,如大量插入.更新数据库会导致锁表,导致连接数过多的异常,此时需要消息队列来缓冲一下.消息队列通过异步处理请求来缓解压力 2 消息队列采用异步通信机制消息队列拥有先 ...