本文转自:http://dorole.com/1422/

使用框架的会话管理工具,也就是本文要说的spring-session,可以理解是替换了Servlet那一套会话管理,既不依赖容器,又不需要改动代码,并且是用了spring-data-redis那一套连接池,可以说是最完美的解决方案。当然,前提是项目要使用Spring Framework才行。

  这里简单记录下整合的过程:

  如果项目之前没有整合过spring-data-redis的话,这一步需要先做,在maven中添加这两个依赖:

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.0.2.RELEASE</version>
</dependency>
 

  再在applicationContext.xml中添加以下bean,用于定义redis的连接池和初始化redis模版操作类,自行替换其中的相关变量。

<!-- redis -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
</bean> <bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.host}" />
<property name="port" value="${redis.port}" />
<property name="password" value="${redis.pass}" />
<property name="timeout" value="${redis.timeout}" />
<property name="poolConfig" ref="jedisPoolConfig" />
<property name="usePool" value="true" />
</bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
</bean> <!-- 将session放入redis -->
<bean id="redisHttpSessionConfiguration"
class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
<property name="maxInactiveIntervalInSeconds" value="1800" />
</bean>

  这里前面几个bean都是操作redis时候使用的,最后一个bean才是spring-session需要用到的,其中的id可以不写或者保持不变,这也是一个约定优先配置的体现。这个bean中又会自动产生多个bean,用于相关操作,极大的简化了我们的配置项。其中有个比较重要的是springSessionRepositoryFilter,它将在下面的代理filter中被调用到。maxInactiveIntervalInSeconds表示超时时间,默认是1800秒。写上述配置的时候我个人习惯采用xml来定义,官方文档中有采用注解来声明一个配置类。

  然后是在web.xml中添加一个session代理filter,通过这个filter来包装Servlet的getSession()。需要注意的是这个filter需要放在所有filter链最前面。

<!-- delegatingFilterProxy -->
<filter>
<filter-name>springSessionRepositoryFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSessionRepositoryFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

  这样便配置完毕了,需要注意的是,spring-session要求Redis Server版本不低于2.8。

  验证:使用redis-cli就可以查看到session key了,且浏览器Cookie中的jsessionid已经替换为session。

最新文章

  1. windows7 64位下环境搭建scrapy爬虫框架
  2. 压力测试报出503错误---ASP.NET支持大并发的相关配置
  3. 绘制n边形:用两个以上的控件来控制矩形的颜色、大小、位置及空实心(程序代写)
  4. xxxx is not translated in zh-rCN, zh-rTW
  5. mysql的几种隐式转化
  6. CodeForcesGym 100753B Bounty Hunter II 二分图最小路径覆盖
  7. Android 编程:calledfromWrongThreadException 的原因
  8. Webpack学习笔记(二)
  9. 【转】JS容器拖拽效果,并通过cookie保存拖拽各容器的所在位置
  10. poj 3321Apple Tree
  11. SPCircleView的使用(圆心向四周扩散动画)
  12. unity图片后期处理
  13. 如何设计一款APP,才能吸引用户眼球
  14. Spring 内部注入bean
  15. [Vijos1130][NOIP2001]数的计数 (递推)
  16. C++/Php/Python 语言执行shell命令
  17. Vue 框架-01- 入门篇 图文教程
  18. crontab练习题
  19. 自定义ScrollView 支持添加头部
  20. uboot配置过程详解1

热门文章

  1. Linux下编辑利器vim,vimrc,viminfo的高级用法
  2. Nginx的HTTPS 301重定向到另一个TLD(托管在同一服务器上)没有显示出SSL警告
  3. PHP-PHP.INI常用配置详解
  4. 使用增强for循环遍历集合的时候操作集合的问题?
  5. 【LeetCode】128. Longest Consecutive Sequence
  6. Android学习系列(7)--App消息通知机制
  7. 常用JS技巧[转]
  8. Javascript-js实现多线程
  9. wait/waitpid函数与僵尸进程、fork 2 times
  10. lua学习笔记13:协程具体解释和举例