Shiro结合Redis解决集群中session同步问题
- pom.xml文件中引入redis的依赖
- 在application.xml配置redis:
- <bean id="jedisConnectionFactory"
- class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
- <property name="poolConfig" ref="jedisPoolConfig" />
- <property name="usePool" value="true" />
- <property name="hostName" value="${redis.host}" />
- <property name="port" value="${redis.port}" />
- <property name="password" value="${redis.password}" />
- <property name="timeout" value="${redis.timeout}" />
- <property name="database" value="${redis.database}" />
- </bean>
- <bean name="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
- <property name="maxIdle" value="${redis.maxIdle}" />
- </bean>
- <!--note: use string template -->
- <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
- lazy-init="false">
- <property name="connectionFactory" ref="jedisConnectionFactory" />
- <property name="keySerializer">
- <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
- </property>
- <property name="valueSerializer">
- <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
- </property>
- </bean>
其中,注意到redisTemplate的序列化属性的配置。
3. shiro配置文件中加入:
- <!-- 使用redis存储管理session -->
- <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
- <!-- 删除失效session -->
- <property name="sessionValidationSchedulerEnabled" value="true" />
- <!-- session失效时间(毫秒) -->
- <property name="globalSessionTimeout" value="1800000" />
- <property name="sessionDAO" ref="sessionDao" />
- </bean>
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <property name="realm" ref="authenticationRealm" />
- <property name="cacheManager" ref="shiroCacheManager" />
- <property name="sessionManager" ref="sessionManager" />
- </bean>
4. 创建SessionDao类继承EnterpriseCacheSessionDAO:
- @Repository("sessionDao")
- public class SessionDao extends EnterpriseCacheSessionDAO {
- @Resource(name = "redisTemplate")
- private RedisTemplate redisTemplate;
- @Override
- protected Serializable doCreate(Session session) {
- Serializable sessionId = super.doCreate(session);
- redisTemplate.boundValueOps("shiro_session_" + sessionId.toString()).set(session, 30, TimeUnit.MINUTES);
- return sessionId;
- }
- @Override
- protected Session doReadSession(Serializable sessionId) {
- Session session = super.doReadSession(sessionId);
- if (session == null) {
- session = (Session) redisTemplate.boundValueOps("shiro_session_" + sessionId.toString()).get();
- }
- return session;
- }
- @Override
- protected void doUpdate(Session session) {
- super.doUpdate(session);
- redisTemplate.boundValueOps("shiro_session_" + session.getId()).set(session, 30, TimeUnit.MINUTES);
- }
- @Override
- protected void doDelete(Session session) {
- redisTemplate.delete("shiro_session_" + session.getId().toString());
- super.doDelete(session);
- }
- }
redisTemplate中boundValueOps为对key的绑定简化的api,可以通过bound封装指定的key,然后进行一系列的操作而无须“显式”的再次指定Key。
5. 4.在本机上同时运行两个相同项目(端口号不同)测试,开启redis服务,登录其中一个项目成功后,redis客户端显示
再登录另一个项目,发现无需登录,redis客户端只存储一个session。
Shiro结合Redis解决集群中session同步问题的更多相关文章
- 集群中Session共享解决方案分析
一.为什么要Session共享 Session存储在服务器的内存中,比如Java中,Session存放在JVM的中,Session也可以持久化到file,MySQL,redis等,SessionID存 ...
- Apache shiro集群实现 (八) web集群时session同步的3种方法
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- redis/分布式文件存储系统/数据库 存储session,解决负载均衡集群中session不一致问题
先来说下session和cookie的异同 session和cookie不仅仅是一个存放在服务器端,一个存放在客户端那么笼统 session虽然存放在服务器端,但是也需要和客户端相互匹配,试想一个浏览 ...
- 关于 tomcat 集群中 session 共享的三种方法
前两种均需要使用 memcached 或redis 存储 session ,最后一种使用 terracotta 服务器共享. 建议使用 redis,不仅仅因为它可以将缓存的内容持久化,还因为它支持的单 ...
- 通过memcached来实现对tomcat集群中Session的共享策略
近期在做一套集群的实现,实现的方案是在Linux下完成对Apache + Tomcat 负载均衡的功能. 上述功能已经实现,有需要了解的朋友可以看我另外一篇博文. Linux下Apache与Tomca ...
- redis之(十五)redis的集群中的哨兵角色
一:redis集群的哨兵的目的是什么?. (1)监控主redis和从redis数据库是否正常运行 (2)主redis出现故障,自动将其中一台从redis升级为主redis.将原先的主redis转变成从 ...
- redis cluster集群中键的分布算法
Redis Cluster Redis Cluster是Redis的作者 Antirez 提供的 Redis 集群方案 —— 官方多机部署方案,每组Redis Cluster是由多个Redis实例组成 ...
- 集群服务器Session同步
事实上,网站总是有状态的.每一个登录信息.用户信息常常被存储在session内部.而当一个网站被部署在不止一台服务器的时候,就会遇到session同步的问题.事实上即使一个很小的网站,也要至少有两台服 ...
- 【转】web集群时session同步的3种方法
转载请注明作者:海底苍鹰地址:http://blog.51yip.com/server/922.html 在做了web集群后,你肯定会首先考虑session同步问题,因为通过负载均衡后,同一个IP访问 ...
随机推荐
- UVa 1600 Patrol Robot【BFS】
题意:给出一个n*m的矩阵,1代表墙,0代表空地,不能连续k次穿过墙,求从起点到达终点的最短路的长度 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰到墙,当前的k减去1,碰到0, ...
- javascript--记忆函数
function memory(val) { if(!memory.cached) {//判断是否创建了缓存 memory.cached = {}; } if(memory.cached[val] ! ...
- 去除input的前后的空格
这里用的是jquery的方法
- PHP实现杨辉三角形
<?php /**** * 杨辉三角形:我的实现方式. * 下标 * 1 0 * 1 1 1 循环上一行数据1次,计算后结果追加到当前行末尾 * 1 2 1 2 * 1 3 3 1 3 * 1 ...
- es6 学习2 模板字符
es6模板字符简直是开发者的福音啊,解决了ES5在字符串功能上的痛点. 1.第一个用途,基本的字符串格式化.将表达式嵌入字符串中进行拼接.用${}来界定 //es5 var name = 'lux' ...
- 2019 前端面试题汇总(主要为 Vue)
原文链接:点我 由于我的技术栈主要为Vue,所以大部分题目都是Vue开发相关的. 1. 谈谈你对MVVM开发模式的理解 MVVM分为Model.View.ViewModel三者. Model:代表数据 ...
- [译] 我最终是怎么玩转了 Vue 的作用域插槽
原文链接:https://juejin.im/post/5c8856e6e51d456b30397f31#comment 原文地址:How I finally got my head around S ...
- JS触发按钮事件
前台代码: <asp:Button ID="btnSaveBattery" runat="server" Text="保存" OnCl ...
- 洛谷 P1020 导弹拦截 (LIS)
第一问最长 不上升子序列,第二问最长上升子序列 套模板就好https://blog.csdn.net/qq_34416123/article/details/81358447 那个神奇的定理当作结论吧 ...
- 三段式状态机 [CPLD/FPGA]
状态机的组成其实比较简单,要素大致有三个:输入,输出,还有状态. 状态机描述时关键是要描述清楚前面提高的几个状态机的要素,即如何进行状态转移:每个状态的输出是什么:状态转移是否和输入条件相关等. 有人 ...