spring-boot 集成spring-session redis
2017-01-09 18:50
597 查看
Spring Session 提供了一套用于管理用户 session 信息的API和实现。
Spring Session为企业级Java应用的session管理带来了革新,使得以下的功能更加容易实现:
编写可水平扩展的原生云应用。
将session所保存的状态卸载到特定的外部session存储中,如Redis或Apache Geode中,它们能够以独立于应用服务器的方式提供高质量的集群。
当用户使用WebSocket发送请求的时候,能够保持HttpSession处于活跃状态。
在非Web请求的处理代码中,能够访问session数据,比如在JMS消息的处理代码中。
支持每个浏览器上使用多个session,从而能够很容易地构建更加丰富的终端用户体验。
控制session id如何在客户端和服务器之间进行交换,这样的话就能很容易地编写Restful API,因为它可以从HTTP 头信息中获取session id,而不必再依赖于cookie。
以上内容,摘自通过Spring Session实现新一代的Session管理,更多内容可以点击阅读。
类中的方法 httpSessionStrategy(),用来定义Spring Session的 HttpSession 集成使用HTTP的头来取代使用 cookie 传送当前session信息。如果使用下面的代码,则是使用cookie来传送 session 信息。
使用cookie,会看到如下信息
启动服务,在chrome浏览器输入 http://127.0.0.1:8080/,得到sessionId
参看链接
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-session-redis
http://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html#boot-spring-configuration
http://docs.spring.io/spring-session/docs/current/reference/html5/guides/rest.html
Spring Session为企业级Java应用的session管理带来了革新,使得以下的功能更加容易实现:
编写可水平扩展的原生云应用。
将session所保存的状态卸载到特定的外部session存储中,如Redis或Apache Geode中,它们能够以独立于应用服务器的方式提供高质量的集群。
当用户使用WebSocket发送请求的时候,能够保持HttpSession处于活跃状态。
在非Web请求的处理代码中,能够访问session数据,比如在JMS消息的处理代码中。
支持每个浏览器上使用多个session,从而能够很容易地构建更加丰富的终端用户体验。
控制session id如何在客户端和服务器之间进行交换,这样的话就能很容易地编写Restful API,因为它可以从HTTP 头信息中获取session id,而不必再依赖于cookie。
以上内容,摘自通过Spring Session实现新一代的Session管理,更多内容可以点击阅读。
Spring-Boot集成Spring session并存入redis
添加maven依赖
redis的相关依赖可以看之前的内容,这里需要增加如下依赖。<dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> </dependency>
Java代码实现
增加HttpSessionConfig。package com.core.config; import org.springframework.context.annotation.Bean; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.session.web.http.HeaderHttpSessionStrategy; import org.springframework.session.web.http.HttpSessionStrategy; @EnableRedisHttpSession(maxInactiveIntervalInSeconds = 100, redisNamespace = "xxxx") public class HttpSessionConfig { @Bean public HttpSessionStrategy httpSessionStrategy() { return new HeaderHttpSessionStrategy(); } }其中注解 EnableRedisHttpSession 创建了一个名为springSessionRepositoryFilter的Spring Bean,该Bean实现了Filter接口。该filter负责通过 Spring Session 替换HttpSession从哪里返回。这里Spring Session是通过 redis 返回。
类中的方法 httpSessionStrategy(),用来定义Spring Session的 HttpSession 集成使用HTTP的头来取代使用 cookie 传送当前session信息。如果使用下面的代码,则是使用cookie来传送 session 信息。
@Bean public HttpSessionStrategy httpSessionStrategy() { return new CookieHttpSessionStrategy(); }使用HTTP的头,会看到如下信息
-- response -- 200 x-auth-token: 4792331e-44c2-4285-a9d1-ebabf0e72251 Content-Type: text/html;charset=UTF-8 Content-Length: 75 Date: Mon, 09 Jan 2017 10:14:00 GMT 8e107efb-bf1e-4a55-b896-c97f629c8e40 : 4792331e-44c2-4285-a9d1-ebabf0e72251
使用cookie,会看到如下信息
-- response -- 200 Set-Cookie: SESSION=4792331e-44c2-4285-a9d1-ebabf0e72251;path=/;HttpOnly Content-Type: text/html;charset=UTF-8 Content-Length: 75 Date: Mon, 09 Jan 2017 10:47:37 GMT
测试
在controller中增加如下代码@GetMapping("/") public String uid(HttpServletRequest request) { HttpSession session = request.getSession(); UUID uid = (UUID) session.getAttribute("uid"); if (uid == null) { uid = UUID.randomUUID(); } session.setAttribute("uid", uid); return uid.toString() + " : " + session.getId(); }
启动服务,在chrome浏览器输入 http://127.0.0.1:8080/,得到sessionId
fbfae849-1d49-4301-b963-573048e763e7在redis中可以看到如下信息
1) "spring:session:xxxx:sessions:fbfae849-1d49-4301-b963-573048e763e7" 2) "spring:session:xxxx:expirations:1483958700000" 3) "spring:session:xxxx:sessions:expires:fbfae849-1d49-4301-b963-573048e763e7"打开火狐的HttpRequester,使用chrome获取的sessionId点击Get,可以看到如下输出
参看链接
https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-session-redis
http://docs.spring.io/spring-session/docs/current/reference/html5/guides/boot.html#boot-spring-configuration
http://docs.spring.io/spring-session/docs/current/reference/html5/guides/rest.html
相关文章推荐
- Redis 命令参考
- 跳跃表
- redis中各种数据类型对应的jedis操作命令
- 一些开源搜索引擎实现——倒排使用原始文件,列存储Hbase,KV store如levelDB、mongoDB、redis,以及SQL的,如sqlite或者xxSQL
- 谈一谈 redis 集群
- php-redis扩展模块安装记录
- centos下部署redis服务环境的操作记录
- redis搭建
- 详解java之redis篇(spring-data-redis整合)
- redis高可用集群介绍
- 基于Redis的CAS服务端集群
- CentOS6.5下Redis安装与配置
- redis集群搭建
- tomcat服务器redis管理session
- redis中缓存的数据与数据库数据一致性的方案(好)
- redis并发处理慢
- Java中使用Jedis操作Redis
- Redis 学习笔记十 发布者订阅者模式与生产者消费者模式
- 六,redis复制
- (转)Spring整合Redis作为缓存