1. pom.xml文件中引入redis的依赖
  2. 在application.xml配置redis:
  1. <bean id="jedisConnectionFactory"
  2. class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  3. <property name="poolConfig" ref="jedisPoolConfig" />
  4. <property name="usePool" value="true" />
  5. <property name="hostName" value="${redis.host}" />
  6. <property name="port" value="${redis.port}" />
  7. <property name="password" value="${redis.password}" />
  8. <property name="timeout" value="${redis.timeout}" />
  9. <property name="database" value="${redis.database}" />
  10. </bean>
  11. <bean name="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
  12. <property name="maxIdle" value="${redis.maxIdle}" />
  13. </bean>
  14. <!--note: use string template -->
  15. <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"
  16. lazy-init="false">
  17. <property name="connectionFactory" ref="jedisConnectionFactory" />
  18. <property name="keySerializer">
  19. <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
  20. </property>
  21. <property name="valueSerializer">
  22. <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
  23. </property>
  24. </bean>

其中,注意到redisTemplate的序列化属性的配置。

3. shiro配置文件中加入:

  1. <!-- 使用redis存储管理session -->
  2. <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
  3. <!-- 删除失效session -->
  4. <property name="sessionValidationSchedulerEnabled" value="true" />
  5. <!-- session失效时间(毫秒) -->
  6. <property name="globalSessionTimeout" value="1800000" />
  7. <property name="sessionDAO" ref="sessionDao" />
  8. </bean>
  1. <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
  2. <property name="realm" ref="authenticationRealm" />
  3. <property name="cacheManager" ref="shiroCacheManager" />
  4. <property name="sessionManager" ref="sessionManager" />
  5. </bean>

4. 创建SessionDao类继承EnterpriseCacheSessionDAO:

  1. @Repository("sessionDao")
  2. public class SessionDao extends EnterpriseCacheSessionDAO {
  3. @Resource(name = "redisTemplate")
  4. private RedisTemplate redisTemplate;
  5. @Override
  6. protected Serializable doCreate(Session session) {
  7. Serializable sessionId = super.doCreate(session);
  8. redisTemplate.boundValueOps("shiro_session_" + sessionId.toString()).set(session, 30, TimeUnit.MINUTES);
  9. return sessionId;
  10. }
  11. @Override
  12. protected Session doReadSession(Serializable sessionId) {
  13. Session session = super.doReadSession(sessionId);
  14. if (session == null) {
  15. session = (Session) redisTemplate.boundValueOps("shiro_session_" + sessionId.toString()).get();
  16. }
  17. return session;
  18. }
  19. @Override
  20. protected void doUpdate(Session session) {
  21. super.doUpdate(session);
  22. redisTemplate.boundValueOps("shiro_session_" + session.getId()).set(session, 30, TimeUnit.MINUTES);
  23. }
  24. @Override
  25. protected void doDelete(Session session) {
  26. redisTemplate.delete("shiro_session_" + session.getId().toString());
  27. super.doDelete(session);
  28. }
  29. }

redisTemplate中boundValueOps为对key的绑定简化的api,可以通过bound封装指定的key,然后进行一系列的操作而无须“显式”的再次指定Key。

5. 4.在本机上同时运行两个相同项目(端口号不同)测试,开启redis服务,登录其中一个项目成功后,redis客户端显示

再登录另一个项目,发现无需登录,redis客户端只存储一个session。

Shiro结合Redis解决集群中session同步问题的更多相关文章

  1. 集群中Session共享解决方案分析

    一.为什么要Session共享 Session存储在服务器的内存中,比如Java中,Session存放在JVM的中,Session也可以持久化到file,MySQL,redis等,SessionID存 ...

  2. Apache shiro集群实现 (八) web集群时session同步的3种方法

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  3. redis/分布式文件存储系统/数据库 存储session,解决负载均衡集群中session不一致问题

    先来说下session和cookie的异同 session和cookie不仅仅是一个存放在服务器端,一个存放在客户端那么笼统 session虽然存放在服务器端,但是也需要和客户端相互匹配,试想一个浏览 ...

  4. 关于 tomcat 集群中 session 共享的三种方法

    前两种均需要使用 memcached 或redis 存储 session ,最后一种使用 terracotta 服务器共享. 建议使用 redis,不仅仅因为它可以将缓存的内容持久化,还因为它支持的单 ...

  5. 通过memcached来实现对tomcat集群中Session的共享策略

    近期在做一套集群的实现,实现的方案是在Linux下完成对Apache + Tomcat 负载均衡的功能. 上述功能已经实现,有需要了解的朋友可以看我另外一篇博文. Linux下Apache与Tomca ...

  6. redis之(十五)redis的集群中的哨兵角色

    一:redis集群的哨兵的目的是什么?. (1)监控主redis和从redis数据库是否正常运行 (2)主redis出现故障,自动将其中一台从redis升级为主redis.将原先的主redis转变成从 ...

  7. redis cluster集群中键的分布算法

    Redis Cluster Redis Cluster是Redis的作者 Antirez 提供的 Redis 集群方案 —— 官方多机部署方案,每组Redis Cluster是由多个Redis实例组成 ...

  8. 集群服务器Session同步

    事实上,网站总是有状态的.每一个登录信息.用户信息常常被存储在session内部.而当一个网站被部署在不止一台服务器的时候,就会遇到session同步的问题.事实上即使一个很小的网站,也要至少有两台服 ...

  9. 【转】web集群时session同步的3种方法

    转载请注明作者:海底苍鹰地址:http://blog.51yip.com/server/922.html 在做了web集群后,你肯定会首先考虑session同步问题,因为通过负载均衡后,同一个IP访问 ...

随机推荐

  1. Idea的一些调试技巧及设置todo

    程序员的工作内容,除了大部分时间写代码之外,因为有不少的时间是用在调试代码上.甚至说不是在调试代码,就是即将调试代码. :) 今天我们来谈谈调试代码的一些技巧,在使用IDE提供的debugger时一些 ...

  2. JavaScript总结(3)

    第3章 获取用户的输入 <script>10 intA=prompt("请输入第一个数字","");11 intB=prompt("请输入 ...

  3. <Sicily>Inversion Number(线段树求逆序数)

    一.题目描述 There is a permutation P with n integers from 1 to n. You have to calculate its inversion num ...

  4. <Sicily>Rails

    一.题目描述 There is a famous railway station in PopPush City. Country there is incredibly hilly. The sta ...

  5. GoldenGate 异常处理预案

    异常处理一般步骤 如果GoldenGate复制出现异常,可以通过以下步骤尝试解决问题: 1)        通过ggsci>view report命令查找ERROR字样,确定错误原因并根据其信息 ...

  6. JTable表格案例

    package com.szht.gpy.frame; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import ...

  7. php实现自动加载类

    PHP 实现自动加载类:

  8. Http协议与TCP协议理解(转载的)

    TCP协议对应于传输层,而HTTP协议对应于应用层,从本质上来说,二者没有可比性.Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页数据的时候,会发出一次Http请求.Http会通 ...

  9. [洛谷P3932]浮游大陆的68号岛

    题目大意:有一行物品,每两个物品之间有一个距离.每个物品有一个价值.现在问你若干问题,每个问题问你把l~r所有物品全部搬到物品x处需要多少价值. 把物品a搬到物品b处的价值为物品a的价值乘a到b的距离 ...

  10. 紫书 例题 10-23 UVa 10213(欧拉公式+高精度)

    用欧拉公式V-E+F=2 V是顶点数,E是边数,F是面数 具体推导见https://blog.csdn.net/QWsin/article/details/53635397 要用高精度 #includ ...