对于最新稳定版本,请使用 Spring Session 4.0.2spring-doc.cadn.net.cn

Reactive Redis Indexed Configurations

要开始使用 RedisIndexedWebSession 支持,您需要将以下依赖项添加到您的项目中:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>
implementation 'org.springframework.session:spring-session-data-redis'

并将其@EnableRedisIndexedWebSession注解添加到一个配置类中:spring-doc.cadn.net.cn

@Configuration
@EnableRedisIndexedWebSession
public class SessionConfig {
    // ...
}

这就是全部内容了。您的应用程序现在支持基于 Redis 的响应式索引会话。 既然您已经配置好了应用程序,也许您可以开始自定义一些东西:spring-doc.cadn.net.cn

使用 JSON 序列化会话

默认情况下,Spring Session Data Redis 使用 Java 序列化来序列化会话属性。 有时候这可能会出现问题,尤其是在多个应用程序使用同一个 Redis 实例但这些应用程序包含不同版本的相同类时。 你可以提供一个 RedisSerializer Bean来自定义如何将会话序列化到 Redis 中。 Spring Data Redis 提供了 GenericJackson2JsonRedisSerializer,它使用 Jackson 的 ObjectMapper 来序列化和反序列化对象。spring-doc.cadn.net.cn

配置Redis序列化器
@Configuration
public class SessionConfig implements BeanClassLoaderAware {

	private ClassLoader loader;

	/**
	 * Note that the bean name for this bean is intentionally
	 * {@code springSessionDefaultRedisSerializer}. It must be named this way to override
	 * the default {@link RedisSerializer} used by Spring Session.
	 */
	@Bean
	public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
		return new GenericJackson2JsonRedisSerializer(objectMapper());
	}

	/**
	 * Customized {@link ObjectMapper} to add mix-in for class that doesn't have default
	 * constructors
	 * @return the {@link ObjectMapper} to use
	 */
	private ObjectMapper objectMapper() {
		ObjectMapper mapper = new ObjectMapper();
		mapper.registerModules(SecurityJackson2Modules.getModules(this.loader));
		return mapper;
	}

	/*
	 * @see
	 * org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(java.lang
	 * .ClassLoader)
	 */
	@Override
	public void setBeanClassLoader(ClassLoader classLoader) {
		this.loader = classLoader;
	}

}

上述代码片段使用了Spring Security,因此我们正在创建一个自定义的ObjectMapper,该自定义对象使用了Spring Security的Jackson模块。 如果没有使用Spring Security Jackson模块,您可以注入您应用程序的ObjectMapper bean,并像这样使用它:spring-doc.cadn.net.cn

@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer(ObjectMapper objectMapper) {
    return new GenericJackson2JsonRedisSerializer(objectMapper);
}

The RedisSerializer bean 名称必须是 springSessionDefaultRedisSerializer,这样它就不会与 Spring Data Redis 使用的其他 RedisSerializer beans 冲突。 如果提供了不同的名称,Spring Session 将无法识别它。spring-doc.cadn.net.cn

指定不同的命名空间

在使用同一个Redis实例的多个应用程序中,或者希望将会话数据与其他存储在Redis中的数据分开的情况下,并不罕见。 因此,Spring Session 使用了一个 0(默认为1) 来在需要时保持会话数据的分离。spring-doc.cadn.net.cn

您可以通过在@EnableRedisIndexedWebSession注解中设置redisNamespace属性来指定namespacespring-doc.cadn.net.cn

指定不同的命名空间
@Configuration
@EnableRedisIndexedWebSession(redisNamespace = "spring:session:myapplication")
public class SessionConfig {
    // ...
}

了解 Spring Session 如何清理过期的会话

Spring Session 依赖于 Redis 空间事件 来清理过期的会话。 具体来说,它监听 __keyevent@*__:expired__keyevent@*__:del 频道上发出的事件,并根据被销毁的关键字来解析会话 ID。spring-doc.cadn.net.cn

作为示例,假设我们有一个会话ID为1234的会话,并且该会话设置为在30分钟后过期。 当过期时间到达时,Redis 将向 __keyevent@*__:expired 通道发出一个事件,消息内容为spring:session:sessions:expires:1234,这是已过期的键。 Spring Session 然后将从键中解析会话ID(1234),并从 Redis 中删除所有相关会话键。spring-doc.cadn.net.cn

依赖于Redis过期时间(expiration)独占性的一个问题是,即使键未被访问,Redis也不会保证何时会触发过期事件。spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

有关详细信息,请参阅Redis文档中的如何过期键spring-doc.cadn.net.cn

为了避免过期事件不一定发生的事实,我们可以确保在预计过期时访问每个键。这意味着如果键的TTL(生存时间)已过期,当尝试访问该键时,Redis会移除该键并触发过期事件。spring-doc.cadn.net.cn

因此,每次会话过期也会通过将会话ID存储在一个按其过期时间排名的有序集合中来跟踪。这使得后台任务可以访问可能已过期的会话,从而更确定地触发Redis的过期事件。spring-doc.cadn.net.cn

例如:spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

ZADD spring:session:sessions:expirations "1.702402961162E12" "648377f7-c76f-4f45-b847-c0268bb48381"

我们不显式地删除键,因为在某些情况下可能会出现竞态条件,错误地将未过期的键识别为已过期。 除非使用分布式锁(这会严重影响我们的性能),否则无法确保过期映射的一致性。 通过简单地访问键,我们可以确保只有在该键的 TTL 过期时才会移除该键。spring-doc.cadn.net.cn

默认情况下,Spring Session 将每 60 秒检索最多 100 个过期会话。 如果您想配置清理任务的运行频率,请参阅 更改会话清理频率 部分。spring-doc.cadn.net.cn

配置 Redis 以发送键空间事件

默认情况下,Spring Session 尝试通过 Redis 的 ConfigureNotifyKeyspaceEventsReactiveAction 来配置发送键空间事件,这可能会将 notify-keyspace-events 配置属性设置为 Egx。 然而,在 Redis 实例已正确安全的情况下,这种策略将不起作用。在这种情况下,Redis 实例应该外部进行配置,并暴露一个类型为 ConfigureReactiveRedisAction.NO_OP 的 Bean 以禁用自动配置。spring-doc.cadn.net.cn

@Bean
public ConfigureReactiveRedisAction configureReactiveRedisAction() {
    return ConfigureReactiveRedisAction.NO_OP;
}

更改会话清理的频率

根据应用程序的需求,您可能希望改变会话清理的频率。 为了做到这一点,您可以暴露一个ReactiveSessionRepositoryCustomizer<ReactiveRedisIndexedSessionRepository> bean,并设置cleanupInterval属性:spring-doc.cadn.net.cn

@Bean
public ReactiveSessionRepositoryCustomizer<ReactiveRedisIndexedSessionRepository> reactiveSessionRepositoryCustomizer() {
    return (sessionRepository) -> sessionRepository.setCleanupInterval(Duration.ofSeconds(30));
}

您也可以将调用设置为disableCleanupTask()以禁用清理任务。spring-doc.cadn.net.cn

@Bean
public ReactiveSessionRepositoryCustomizer<ReactiveRedisIndexedSessionRepository> reactiveSessionRepositoryCustomizer() {
    return (sessionRepository) -> sessionRepository.disableCleanupTask();
}

掌控清理任务

有时,默认的清理任务可能不足以满足您的应用程序需求。 您可能希望采用不同的策略来清理过期的会话。 由于您知道 会话 ID 存储在排序集合中,键为 spring:session:sessions:expirations,并按其过期时间进行排名,您可以 禁用默认清理 任务并提供自己的策略。 例如:spring-doc.cadn.net.cn

@Component
public class SessionEvicter {

    private ReactiveRedisOperations<String, String> redisOperations;

    @Scheduled
    public Mono<Void> cleanup() {
        Instant now = Instant.now();
        Instant oneMinuteAgo = now.minus(Duration.ofMinutes(1));
        Range<Double> range = Range.closed((double) oneMinuteAgo.toEpochMilli(), (double) now.toEpochMilli());
        Limit limit = Limit.limit().count(1000);
        return this.redisOperations.opsForZSet().reverseRangeByScore("spring:session:sessions:expirations", range, limit)
                // do something with the session ids
                .then();
    }

}

监听会话事件

经常来说,响应会话事件是非常有价值的。例如,你可能希望根据会话生命周期执行某种类型的处理。spring-doc.cadn.net.cn

您配置应用程序监听SessionCreatedEventSessionDeletedEventSessionExpiredEvent事件。 在Spring中,有几种方式可以监听应用事件,对于这个示例,我们将使用@EventListener注解。 了解更多信息spring-doc.cadn.net.cn

@Component
public class SessionEventListener {

    @EventListener
    public Mono<Void> processSessionCreatedEvent(SessionCreatedEvent event) {
        // do the necessary work
    }

    @EventListener
    public Mono<Void> processSessionDeletedEvent(SessionDeletedEvent event) {
        // do the necessary work
    }

    @EventListener
    public Mono<Void> processSessionExpiredEvent(SessionExpiredEvent event) {
        // do the necessary work
    }

}