首页 > spring boot 怎么把session 放到redis

spring boot 怎么把session 放到redis

通过第三方登录接口验证通过后返回登录信息,我将登录信息的用户 id 和 ticket 作为键和值存在 session 中

具体是这样子:

HttpSession session= request.getSession();
session.setAttribute(UserId,ticket);

现在想 将这个session 存到redis.

具体的准备差不多按照 http://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html 配置了

还需要做什么 不太懂。


直接在启动类上加入@EnableRedisHttpSession注解即可,引用一段spring boot官方文档的内容,

Spring Session provides support for managing a user’s session information. If you are writing a web application and Spring Session and Spring Data Redis are both on the classpath, Spring Boot will auto-configure Spring Session through its @EnableRedisHttpSession. Session data will be stored in Redis and the session timeout can be configured using the server.session.timeout property.


如果你的redis安装正确,spring的部分也按照配置完成了,那你运行下试试,理论上不需要再做什么额外的东西了。

你原先的代码,类似:

HttpSession session= request.getSession();
session.setAttribute(UserId,ticket);

不需要有任何变动,因为The Spring configuration is responsible for creating a Servlet Filter that replaces the HttpSession implementation with an implementation backed by Spring Session,所以,面向接口编程真的好灵活,有木有!^^


建议在web容器上做配置~

1.jetty-session-redis
2.https://github.com/jcoleman/tomcat-redis-session-manager

【热门文章】
【热门文章】