Spring Session Data Redis
一. spring session简介
1. 什么是Spring Session
spring-session是spring大家庭下的一个子项目,她的出现是为了集中管理session会话,解决多应用环境下的session共享问题,虽然她是spring框架下的子项目,但是spring-session不依赖于spring框架,可以将spring-session用于其他框架下提供session管理能力。
2. 主要作用(解决什么问题)
Spring Session 提供了用于管理用户会话信息的 API 和实现
3. 特征
Spring Session 使得支持集群会话变得微不足道,而无需绑定到应用程序容器特定的解决方案。它还提供与以下内容的透明集成
- HttpSession 允许以中立方式替换应用程序容器(即 Tomcat)中的 HttpSession,支持在标头中提供会话 ID 以使用 RESTful API
- WebSocket 提供在接收 WebSocket 消息时保持 HttpSession 活动的能力
- WebSession 允许以应用程序容器中立的方式替换 Spring WebFlux 的 WebSession
4. 包含模块
- Spring Session Core - 提供核心 Spring Session 功能和 API
- Spring Session Data Redis - 提供由 Redis 和配置支持支持的 SessionRepository 和 ReactiveSessionRepository 实现
- Spring Session JDBC - 提供由关系数据库和配置支持支持的 SessionRepository 实现
- Spring Session Hazelcast - 提供由 Hazelcast 和配置支持支持的 SessionRepository 实现
5. 使用
maven引入对应的模块(以Spring Session Data Redis为例)
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
二. Spring Session Data Redis
文档地址 https://docs.spring.io/spring-session/reference/samples.html
1. 使用redis的HttpsSession代替原本的Spring Session
1. 更新依赖
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
2.修改spring session配置
#src/main/resources/application.properties
spring.session.store-type=redis # Session store type.
在底层,Spring Boot 应用的配置相当于手动添加@EnableRedisHttpSession
注解。这将创建一个名为springSessionRepositoryFilter
implements的 Spring bean Filter
。过滤器负责替换HttpSession
Spring Session 支持的实现。
使用 可以进一步定制application.properties
,如以下清单所示:
server.servlet.session.timeout = #会话超时。如果未指定持续时间后缀,则使用秒。
spring.session.redis.flush-mode=on_save # 会话刷新模式。
spring.session.redis.namespace=spring:session # 用于存储会话的键的命名空间。
3. 配置redis链接
spring.redis.host=localhost #Redis 服务器主机。
spring.redis.password= #redis服务器的登录密码。
spring.redis.port=6379 # Redis 服务器端口。
2. 修改默认的JdkSerializationRedisSerializer序列化器
1.官方文档介绍10.8 https://docs.spring.io/spring-data/data-redis/docs/2.6.2/reference/html/#reference
从框架的角度来看,Redis 中存储的数据只是字节。虽然 Redis 本身支持各种类型,但在大多数情况下,这些类型指的是数据的存储方式,而不是它所代表的内容。由用户决定是否将信息转换为字符串或任何其他对象。
在 Spring Data 中,用户(自定义)类型和原始数据(反之亦然)之间的转换由org.springframework.data.redis.serializer包中的 Redis 处理。
这个包包含两种类型的序列化器,顾名思义,它们负责序列化过程:
基于RedisSerializer.
RedisElementReader使用和的元素读取器和写入器RedisElementWriter。
这些变体之间的主要区别在于RedisSerializer主要序列化到byte[]而读者和作者使用ByteBuffer.
有多种实现可用(包括本文档中已经提到的两种):
JdkSerializationRedisSerializer, 默认情况下用于RedisCache和RedisTemplate。
StringRedisSerializer. _
但是,可以OxmSerializer通过 Spring OXM支持用于对象/XML 映射,Jackson2JsonRedisSerializer或者GenericJackson2JsonRedisSerializer以JSON格式存储数据。
请注意,存储格式不仅限于值。它可以用于键、值或散列,没有任何限制。
2. 自定义序列化器配置
github参考地址 https://github.com/spring-projects/spring-session/blob/main/spring-session-samples/spring-session-sample-boot-redis-json/src/main/java/sample/config/SessionConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@Configuration
@EnableRedisHttpSession
public class SpringSessionRedisConfig {
/**
* remark:json序列化
*/
@Bean
public RedisSerializer springSessionDefaultRedisSerializer() {
return new GenericJackson2JsonRedisSerializer();
}
}
3. 自定义cookie配置
1. 文档地址 https://docs.spring.io/spring-session/reference/guides/java-custom-cookie.html#page-title
2. 介绍及使用
设置 Spring Session 后,您可以通过定制CookieSerializer
为 Spring bean 来自定义会话 cookie 的编写方式。Spring Session 附带DefaultCookieSerializer
. 当DefaultCookieSerializer
您使用诸如@EnableRedisHttpSession
. 下面的例子展示了如何自定义 Spring Session 的 cookie
//这个bean放到SpringSessionRedisConfig配置中。
@Bean
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer serializer = new DefaultCookieSerializer();
serializer.setCookieName("JSESSIONID");
serializer.setCookiePath("/");
serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
return serializer;
}
- 将 cookie 的名称自定义为
JSESSIONID
. - 将 cookie 的路径自定义为
/
(而不是上下文根的默认路径)。 - 我们将域名模式(正则表达式)自定义为
^.+?\\.(\\w+\\.[a-z]+)$
. 这允许跨域和应用程序共享会话。如果正则表达式不匹配,则不设置域并使用现有域。如果正则表达式匹配,则将第一个分组用作域。这意味着对https://child.example.com的请求会将域设置为example.com
. 但是,对http://localhost:8080/或https://192.168.1.100:8080/的请求会使 cookie 未设置,因此仍可在开发中工作,而无需对生产进行任何更改。
3.可选配置选项
cookieName
: 要使用的 cookie 的名称。默认值:SESSION
.useSecureCookie
:指定是否应使用安全 cookie。默认值:使用HttpServletRequest.isSecure()
创建时的值。cookiePath
: cookie 的路径。默认值:上下文根。cookieMaxAge
:指定在创建会话时要设置的 cookie 的最大年龄。默认值:-1
,表示关闭浏览器时应删除 cookie。jvmRoute
:指定要附加到会话 ID 并包含在 cookie 中的后缀。用于标识要路由到哪个 JVM 以实现会话亲和性。对于某些实现(即 Redis),此选项不会提供任何性能优势。但是,它可以帮助跟踪特定用户的日志。domainName
:允许指定用于 cookie 的特定域名。此选项易于理解,但通常需要在开发环境和生产环境之间进行不同的配置。将其domainNamePattern
视为替代方案。domainNamePattern
: 一种不区分大小写的模式,用于从HttpServletRequest#getServerName()
. 该模式应提供用于提取 cookie 域值的单个分组。如果正则表达式不匹配,则不设置域并使用现有域。如果正则表达式匹配,则将第一个分组用作域。sameSite
:SameSite
cookie 指令的值。要禁用SameSite
cookie 指令的序列化,您可以将此值设置为null
。默认:Lax
- 本文标签: Spring session
- 本文链接: https://www.tianyajuanke.top/article/29
- 版权声明: 本文由吴沛芙原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权