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. jquery实现上下浮动

    jquery实现上下浮动: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  2. RMAN备份脚本--DataGuard primary

    单机环境全备   export ORACLE_BASE=/oracle export ORACLE_HOME=$ORACLE_BASE/product/10.2.0/db_1 export ORACL ...

  3. Monad的基本运算

    A monad is created by defining a type constructor M and two operations, bind and return (where retur ...

  4. SSD-tensorflow-1 demo

    一.简易识别 用最简单的已训练好的模型对20类目标做检测. 你电脑的tensorflow + CUDA + CUDNN环境都是OK的, 同时python需要安装cv2库 {      'aeropla ...

  5. Rman备份及不完全恢复操作

    最后更新时间:2018/12/18 启用归档 --检查是否为归档 SQL> archive log list; Database log mode              No Archive ...

  6. Git学习笔记 1,GitHub常用命令1

    廖雪峰Git教程 莫烦Git教程 莫烦Git视频教程 --------------- init > apt-get install git # 安装 > mkdir /home/yzn_g ...

  7. 紫书 习题 10-11 UVa 1646(斐波那契+高精度)

    自己用手算一下可以发现是斐波那契数列,然后因为数字很大,用高精度 以后做题的时候记得算几个数据找规律 #include<cstdio> #include<cmath> #inc ...

  8. 紫书 例题 10-21 UVa 11971(连续概率)

    感觉这道题的转换真的是神来之笔 把木条转换成圆,只是切得次数变多一次 然后只要有一根木条长度为直径就租不成 其他点的概率为1/2^k 当前这个点的有k+1种可能 所以答案为1 - (k+1)/2^k ...

  9. 题解 P2195 【HXY造公园】

    天哪这道题竟然只有一篇题解! emm,首先读题看完两个操作就已经有很明确的思路了,显然是并查集+树的直径 一波解决. 并查集不多说了,如果不了解的可以看这里. 树的直径的思路很朴实,就是两边DFS(B ...

  10. 转载-- Qt Creator编译时make: arm-linux-g++: command not found 错误!

    前提是已经配置好交叉编译器,但是qt creator找不到. 解决方法: 修改 /usr/local/Trolltech/QtEmbedded-4.7.0-arm/mkspecs/qws/linux- ...