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 ...
随机推荐
- git版本控制系统重新认识
git 版本控制系统 目标:完全搞懂git分布式版本控制系统 搭建git版本控制系统 cvs集中化版本控制系统--集中式管理的服务器 git分布式版本控制系统--会将原始代码仓库镜像下来 新项目使用g ...
- php上传文件报错以及对应代号信息-转载http://jewel-m.iteye.com/blog/1210344
用PHP上传文件时,我们会用程序去监听浏览器发送过来的文件信息,首先会通 过$_FILES[fieldName]['error']的不同数值来判断此欲上传的文件状态是否正常.$_FILES[field ...
- css 层叠 比较特殊性
css 层叠: 多个相同的css声明(属性),应用到同一个元素上.当一个标签声明冲突时,浏览器会自动出发层叠机制 1:比较优先级 2:比较特殊性 3:比较源次序 依次经过上面的1,2,3的比较后,最终 ...
- vue-cli项目开发运行时内存暴涨卡死电脑
最近开发一个vue项目时遇到电脑卡死问题,突然间系统就非常卡,然后卡着卡着就死机了,鼠标也动不了了,只能冷启动.而且因为是突然卡死,没来得及打开任务管理器. 最开始以为是硬盘的问题,但是在卡死几次后, ...
- Fortify漏洞之Path Manipulation(路径篡改)
继续对Fortify的漏洞进行总结,本篇主要针对 Path Manipulation(路径篡改)的漏洞进行总结,如下: 1.Path Manipulation(路径篡改) 1.1.产生原因: 当满足以 ...
- Android NDK 学习之接受Java传入的字符串
本博客主要是在Ubuntu 下开发,且默认你已经安装了Eclipse,Android SDK, Android NDK, CDT插件. 在Eclipse中添加配置NDK,路径如下Eclipse-> ...
- ipv4与ipv6 Inet4Address类和Inet6Address类
在设置本地IP地址的时候,一些人会疑惑IPv4与IPv6的区别是什么?下面由学习啦小编为你分享ipv4与ipv6的区别的相关内容,希望对大家有所帮助. ipv4与ipv6的区别 在windows 7以 ...
- redis过期事件回调函数,与有序集合
https://cloud.tencent.com/developer/article/1347437 python中的Redis键空间通知(过期回调) set notify-keyspace-ev ...
- SQL SERVER-邮件配置
存储发邮件 USE msdb GO EXEC sp_send_dbmail @profile_name = 'mail_pro', @recipients='Jinwei.chang@quantacn ...
- centos8 安装 mongodb 4.2 (使用yum)
1.制作 repo 文件 参考 mongodb 官方的安装文档,使用下面的脚本制作Yum库安装mongodb4.2,但安装过程提示 "Failed to synchronize cache ...