跳到主要内容

Redis 配置

QWen Max 中英对照 Redis HTTP Session Redis Configurations

现在你已经配置好了应用程序,你可能想要开始自定义一些内容:

使用 JSON 序列化会话

默认情况下,Spring Session 使用 Java 序列化来序列化会话属性。有时这可能会出现问题,特别是当你有多个应用程序使用同一个 Redis 实例但具有同一类的不同版本时。你可以提供一个 RedisSerializer bean 来自定义会话如何被序列化到 Redis 中。Spring Data Redis 提供了 GenericJackson2JsonRedisSerializer,它使用 Jackson 的 ObjectMapper 来序列化和反序列化对象。

@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;
}

}
java

上述代码片段使用了 Spring Security,因此我们创建了一个自定义的 ObjectMapper,它使用了 Spring Security 的 Jackson 模块。如果你不需要 Spring Security 的 Jackson 模块,你可以注入你应用程序的 ObjectMapper bean 并像这样使用:

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

指定不同的命名空间

在多个应用程序使用相同的 Redis 实例时,这种情况并不少见。因此,Spring Session 使用一个 namespace(默认为 spring:session)来根据需要隔离会话数据。

使用 Spring Boot 属性

你可以通过设置 spring.session.redis.namespace 属性来指定它。

spring.session.redis.namespace=spring:session:myapplication
properties
spring:
session:
redis:
namespace: "spring:session:myapplication"
yml

使用注解的属性

你可以通过在 @EnableRedisHttpSession@EnableRedisIndexedHttpSession@EnableRedisWebSession 注解中设置 redisNamespace 属性来指定 namespace

@Configuration
@EnableRedisHttpSession(redisNamespace = "spring:session:myapplication")
public class SessionConfig {
// ...
}
java
@Configuration
@EnableRedisIndexedHttpSession(redisNamespace = "spring:session:myapplication")
public class SessionConfig {
// ...
}
java
@Configuration
@EnableRedisWebSession(redisNamespace = "spring:session:myapplication")
public class SessionConfig {
// ...
}
java

RedisSessionRepositoryRedisIndexedSessionRepository 之间进行选择

在使用 Spring Session Redis 时,你可能需要在 RedisSessionRepositoryRedisIndexedSessionRepository 之间进行选择。两者都是 SessionRepository 接口的实现,用于将会话数据存储在 Redis 中。然而,它们在处理会话索引和查询方面有所不同。

  • RedisSessionRepository: RedisSessionRepository 是一个基本的实现,它将会话数据存储在 Redis 中,没有任何额外的索引。它使用简单的键值结构来存储会话属性。每个会话都被分配了一个唯一的会话 ID,并且会话数据存储在与该 ID 关联的 Redis 键下。当需要检索会话时,仓库使用会话 ID 查询 Redis 以获取相关的会话数据。由于没有索引,基于属性或其他非会话 ID 的标准查询会话可能会效率低下。

  • RedisIndexedSessionRepository: RedisIndexedSessionRepository 是一个扩展的实现,为存储在 Redis 中的会话提供了索引功能。它在 Redis 中引入了额外的数据结构,以便根据属性或标准高效地查询会话。除了 RedisSessionRepository 使用的键值结构外,它还维护了额外的索引来实现快速查找。例如,它可能会根据会话属性(如用户 ID 或最后访问时间)创建索引。这些索引允许根据特定标准高效地查询会话,从而提高性能并启用高级会话管理功能。此外,RedisIndexedSessionRepository 还支持会话过期和删除。

警告

当使用 RedisIndexedSessionRepository 与 Redis 集群时,您必须注意 它仅订阅集群中一个随机 Redis 节点的事件,如果事件发生在其他节点上,这可能导致某些会话索引未被清理。

配置 RedisSessionRepository

使用 Spring Boot 属性

如果你使用的是 Spring Boot,RedisSessionRepository 是默认实现。但是,如果你想明确指定它,可以在你的应用程序中设置以下属性:

spring.session.redis.repository-type=default
properties
spring:
session:
redis:
repository-type: default
yml

使用注释

你可以使用 @EnableRedisHttpSession 注解来配置 RedisSessionRepository

@Configuration
@EnableRedisHttpSession
public class SessionConfig {
// ...
}
java

配置 RedisIndexedSessionRepository

使用 Spring Boot 属性

您可以通过在应用程序中设置以下属性来配置 RedisIndexedSessionRepository

spring.session.redis.repository-type=indexed
properties
spring:
session:
redis:
repository-type: indexed
yml

使用注解

你可以通过使用 @EnableRedisIndexedHttpSession 注解来配置 RedisIndexedSessionRepository

@Configuration
@EnableRedisIndexedHttpSession
public class SessionConfig {
// ...
}
java

监听会话事件

很多时候,对会话事件做出反应是非常有价值的,例如,你可能希望根据会话生命周期进行某种处理。为了能够做到这一点,你必须使用indexed repository。如果你不知道索引仓库和默认仓库之间的区别,可以参考这一节

配置好索引存储库后,你现在可以开始监听 SessionCreatedEventSessionDeletedEventSessionDestroyedEventSessionExpiredEvent 事件。在 Spring 中有几种方式可以监听应用程序事件,我们将使用 @EventListener 注解。

@Component
public class SessionEventListener {

@EventListener
public void processSessionCreatedEvent(SessionCreatedEvent event) {
// do the necessary work
}

@EventListener
public void processSessionDeletedEvent(SessionDeletedEvent event) {
// do the necessary work
}

@EventListener
public void processSessionDestroyedEvent(SessionDestroyedEvent event) {
// do the necessary work
}

@EventListener
public void processSessionExpiredEvent(SessionExpiredEvent event) {
// do the necessary work
}

}
java

查找特定用户的所有会话

通过检索特定用户的所有会话,您可以跟踪该用户在不同设备或浏览器上的活动会话。例如,您可以将这些信息用于会话管理目的,比如允许用户使特定会话失效或登出,或者根据用户的会话活动执行相应操作。

要做到这一点,首先你必须使用indexed repository,然后你可以注入FindByIndexNameSessionRepository接口,如下所示:

@Autowired
public FindByIndexNameSessionRepository<? extends Session> sessions;

public Collection<? extends Session> getSessions(Principal principal) {
Collection<? extends Session> usersSessions = this.sessions.findByPrincipalName(principal.getName()).values();
return usersSessions;
}

public void removeSession(Principal principal, String sessionIdToDelete) {
Set<String> usersSessionIds = this.sessions.findByPrincipalName(principal.getName()).keySet();
if (usersSessionIds.contains(sessionIdToDelete)) {
this.sessions.deleteById(sessionIdToDelete);
}
}
java

在上面的示例中,你可以使用 getSessions 方法来查找特定用户的所有会话,并使用 removeSession 方法来移除用户的特定会话。

配置 Redis 会话映射器

Spring Session Redis 从 Redis 中检索会话信息,并将其存储在 Map<String, Object> 中。这个映射需要经过一个映射过程,才能转换成 MapSession 对象,然后在 RedisSession 中使用。

用于此目的的默认映射器称为 RedisSessionMapper。如果会话映射中不包含构建会话所需的最小必要键,如 creationTime,则该映射器将抛出异常。缺少所需键的一种可能情况是,由于过期等原因,在保存过程进行期间会话键被并发删除。这是因为使用了 HSET 命令 来设置键内的字段,如果键不存在,该命令将创建它。

如果你想要自定义映射过程,你可以创建自己的 BiFunction<String, Map<String, Object>, MapSession> 实现,并将其设置到 session 仓库中。下面的示例展示了如何将映射过程委托给默认的映射器,但如果抛出异常,则会从 Redis 中删除该 session:

@Configuration
@EnableRedisHttpSession
public class SessionConfig {

@Bean
SessionRepositoryCustomizer<RedisSessionRepository> redisSessionRepositoryCustomizer() {
return (redisSessionRepository) -> redisSessionRepository
.setRedisSessionMapper(new SafeRedisSessionMapper(redisSessionRepository));
}

static class SafeRedisSessionMapper implements BiFunction<String, Map<String, Object>, MapSession> {

private final RedisSessionMapper delegate = new RedisSessionMapper();

private final RedisSessionRepository sessionRepository;

SafeRedisSessionMapper(RedisSessionRepository sessionRepository) {
this.sessionRepository = sessionRepository;
}

@Override
public MapSession apply(String sessionId, Map<String, Object> map) {
try {
return this.delegate.apply(sessionId, map);
}
catch (IllegalStateException ex) {
this.sessionRepository.deleteById(sessionId);
return null;
}
}

}

}
java

自定义会话过期存储

由于 Redis 的特性,如果键未被访问,则无法保证何时会触发过期事件。更多详情,请参阅 Redis 文档中的键过期部分。

为了减轻过期事件的不确定性,会话还存储了它们的预期过期时间。这确保了每个键可以在其预期过期时被访问。RedisSessionExpirationStore 接口定义了跟踪会话及其过期时间的通用操作,并提供了一种清理过期会话的策略。

默认情况下,每个会话的过期时间会被追踪到最接近的分钟。这允许后台任务访问可能已过期的会话,以确保 Redis 过期事件以更确定的方式触发。

例如:

SADD spring:session:expirations:1439245080000 expires:33fdd1b6-b496-4b33-9f7d-df96679d32fe
EXPIRE spring:session:expirations:1439245080000 2100
none

后台任务将使用这些映射来显式请求每个会话过期键。通过访问该键而不是删除它,我们确保只有在 TTL 过期时 Redis 才会为我们删除该键。

通过自定义会话过期存储,您可以根据需要更有效地管理会话过期。为此,您应该提供一个类型为 RedisSessionExpirationStore 的 Bean,该 Bean 将被 Spring Session Data Redis 配置所采用:

import org.springframework.session.data.redis.SortedSetRedisSessionExpirationStore;

@Configuration
@EnableRedisIndexedHttpSession
public class SessionConfig {

@Bean
public RedisSessionExpirationStore redisSessionExpirationStore(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(RedisSerializer.string());
redisTemplate.setHashKeySerializer(RedisSerializer.string());
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.afterPropertiesSet();
return new SortedSetRedisSessionExpirationStore(redisTemplate, RedisIndexedSessionRepository.DEFAULT_NAMESPACE);
}

}
java

在上面的代码中,使用了 SortedSetRedisSessionExpirationStore 实现,该实现使用 有序集合 来存储带有其过期时间作为分数的会话 ID。

备注

我们不会显式地删除密钥,因为在某些情况下,可能会出现竞态条件,错误地将未过期的密钥识别为已过期。除了使用分布式锁(这会严重影响性能)之外,没有其他方法可以确保过期映射的一致性。通过简单地访问密钥,我们可以确保只有在该密钥的TTL过期时才会被移除。然而,在您的实现中,您可以选择最适合的策略。