此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Session 3.5.2spring-doc.cadn.net.cn

常见配置

本节包含适用于所有或大多数 Spring Session 模块的常见配置。 它包含以下用例的配置示例:spring-doc.cadn.net.cn

更改会话 ID 的生成方式

默认情况下,Spring Session 使用UuidSessionIdGenerator这反过来又使用java.util.UUID以生成会话 ID。 在某些情况下,最好包含其他字符以增加熵,或者您可能希望使用不同的算法来生成会话 ID。 要更改此设置,您可以提供自定义SessionIdGenerator豆:spring-doc.cadn.net.cn

更改会话 ID 的生成方式
@Bean
public SessionIdGenerator sessionIdGenerator() {
    return new MySessionIdGenerator();
}

class MySessionIdGenerator implements SessionIdGenerator {

    @Override
    public String generate() {
        // ...
    }

}

暴露您的SessionIdGeneratorbean,Spring Session 将使用它来生成会话 ID。spring-doc.cadn.net.cn

如果您手动配置SessionRepositorybean(而不是使用@EnableRedisHttpSession),您可以将SessionIdGenerator直接在SessionRepository实现:spring-doc.cadn.net.cn

设置SessionIdGenerator直接进入SessionRepository实现
@Bean
public RedisSessionRepository redisSessionRepository(RedisOperations redisOperations) {
    RedisSessionRepository repository = new RedisSessionRepository(redisOperations)
    repository.setSessionIdGenerator(new MySessionIdGenerator());
    return repository;
}

设置 Spring Session 后,您可以通过公开CookieSerializer作为春豆。 春季会话附带DefaultCookieSerializer. 公开DefaultCookieSerializer作为 Spring bean 在您使用@EnableRedisHttpSession. 以下示例显示了如何自定义 Spring Session 的 cookie:spring-doc.cadn.net.cn

	@Bean
	public CookieSerializer cookieSerializer() {
		DefaultCookieSerializer serializer = new DefaultCookieSerializer();
		serializer.setCookieName("JSESSIONID"); (1)
		serializer.setCookiePath("/"); (2)
		serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$"); (3)
		return serializer;
	}
1 我们将 cookie 的名称自定义为JSESSIONID.
2 我们将 cookie 的路径自定义为 (而不是上下文根的默认值)。/
3 我们将域名模式(正则表达式)自定义为^.?\\.(\\w\\.[a-z]+)$. 这允许跨域和应用程序共享会话。 如果正则表达式不匹配,则不设置域,使用现有域。 如果正则表达式匹配,则使用第一个分组作为域。 这意味着对 child.example.com 的请求会将域设置为example.com. 但是,对 localhost:8080/192.168.1.100:8080/ 的请求会使 cookie 保持未设置状态,因此,在开发中仍然可以工作,而无需对生产进行任何更改。
您应该只匹配有效的域字符,因为域名会反映在响应中。 这样做可以防止恶意用户执行 HTTP 响应拆分等攻击。

以下配置选项可用:spring-doc.cadn.net.cn

  • cookieName:要使用的 Cookie 的名称。 违约:SESSION.spring-doc.cadn.net.cn

  • useSecureCookie:指定是否应使用安全 Cookie。 默认值:使用HttpServletRequest.isSecure()在创作时。spring-doc.cadn.net.cn

  • cookiePath:Cookie 的路径。 默认值:上下文根。spring-doc.cadn.net.cn

  • cookieMaxAge:指定创建会话时要设置的 Cookie 的最大期限。 违约:-1,这表示在浏览器关闭时应删除 cookie。spring-doc.cadn.net.cn

  • jvmRoute:指定要附加到会话 ID 并包含在 Cookie 中的后缀。 用于标识要路由到哪个 JVM 以实现会话关联性。 对于某些实现(即 Redis),此选项不提供性能优势。 但是,它可以帮助跟踪特定用户的日志。spring-doc.cadn.net.cn

  • domainName:允许指定要用于 cookie 的特定域名。 此选项易于理解,但通常需要在开发环境和生产环境之间进行不同的配置。 看domainNamePattern作为替代方案。spring-doc.cadn.net.cn

  • domainNamePattern:一种不区分大小写的模式,用于从HttpServletRequest#getServerName(). 该模式应提供用于提取 cookie 域值的单个分组。 如果正则表达式不匹配,则不设置域,使用现有域。 如果正则表达式匹配,则使用第一个分组作为域。spring-doc.cadn.net.cn

  • sameSite:的值SameSitecookie 指令。 要禁用SameSitecookie 指令,您可以将此值设置为null. 违约:Laxspring-doc.cadn.net.cn

  • rememberMeRequestAttribute:指示记住我登录的请求属性名称。 如果指定,则 cookie 将写入为Integer.MAX_VALUE.spring-doc.cadn.net.cn

如果您正在使用SpringSessionRememberMeServices并且您正在声明一个自定义DefaultCookieSerializerbean,则应将rememberMeRequestAttribute字段,以确保 Spring Session 依赖于会话过期而不是 cookie 过期。 为此,您可以使用以下代码片段:defaultCookieSerializer.setRememberMeRequestAttribute(SpringSessionRememberMeServices.REMEMBER_ME_LOGIN_ATTR);spring-doc.cadn.net.cn

您可以通过公开WebSessionIdResolver作为春豆。 Spring Session 使用CookieWebSessionIdResolver默认情况下。 以下示例显示了如何自定义 Spring Session 的 cookie:spring-doc.cadn.net.cn

	@Bean
	public WebSessionIdResolver webSessionIdResolver() {
		CookieWebSessionIdResolver resolver = new CookieWebSessionIdResolver();
		resolver.setCookieName("JSESSIONID"); (1)
		resolver.addCookieInitializer((builder) -> builder.path("/")); (2)
		resolver.addCookieInitializer((builder) -> builder.sameSite("Strict")); (3)
		return resolver;
	}
1 我们将 cookie 的名称自定义为JSESSIONID.
2 我们将 cookie 的路径自定义为 (而不是上下文根的默认值)。/
3 我们定制SameSitecookie 指令设置为Strict.

提供 Spring Session 实现ReactiveSessionRegistry

Spring Session 提供与 Spring Security 的集成,以支持其响应式并发会话控制。 这允许限制单个用户可以同时拥有的活动会话数量,但是,与默认的 Spring Security 支持不同,这也适用于集群环境。 这是通过提供SpringSessionBackedReactiveSessionRegistry实现 Spring Security 的ReactiveSessionRegistry接口。spring-doc.cadn.net.cn

将 SpringSessionBackedReactiveSessionRegistry 定义为 bean
@Bean
public <S extends Session> SpringSessionBackedReactiveSessionRegistry<S> sessionRegistry(
        ReactiveSessionRepository<S> sessionRepository,
        ReactiveFindByIndexNameSessionRepository<S> indexedSessionRepository) {
    return new SpringSessionBackedReactiveSessionRegistry<>(sessionRepository, indexedSessionRepository);
}

请参阅 Spring Security 并发会话控制文档,了解更多使用ReactiveSessionRegistry. 您还可以在此处查看示例应用程序。spring-doc.cadn.net.cn