|
此版本仍在开发中,尚未被认为是稳定的。请使用最新的稳定版本 Spring Session 4.0.2! |
Redis 配置
现在您已经配置好了应用程序,可能想要开始自定义一些内容:
-
我想使用 Spring Boot 属性来自定义 Redis 配置
-
我想在选择
RedisSessionRepository或RedisIndexedSessionRepository时得到帮助帮助。 -
我想指定一个不同的命名空间这里。
-
我想知道会话何时被创建、删除、销毁或过期。
-
我想查找特定用户的所有会话
-
我想安全地反序列化Redis会话安全反序列化Redis会话
-
自定义会话过期存储
使用 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 JacksonJsonRedisSerializer<>(objectMapper(), Object.class);
}
/**
* Customized {@link JsonMapper} to add mix-in for class that doesn't have default
* constructors
* @return the {@link JsonMapper} to use
*/
private JsonMapper objectMapper() {
return JsonMapper.builder().addModules(SecurityJacksonModules.getModules(this.loader)).build();
}
/*
* @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,并像这样使用它:
@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer(ObjectMapper objectMapper) {
return new GenericJackson2JsonRedisSerializer(objectMapper);
}
指定不同的命名空间
在同一台Redis实例上使用多个应用程序的情况并不少见。
因此,Spring Session 使用一个 0(默认为 来在需要时将会话数据隔离开来。spring:session)
使用 Spring Boot 属性
您可以通过设置spring.session.redis.namespace属性来指定它。
spring.session.redis.namespace=spring:session:myapplication
spring:
session:
redis:
namespace: "spring:session:myapplication"
使用注解的属性
您可以通过在redisNamespace注解的namespace属性、@EnableRedisHttpSession注解的redisNamespace属性或@EnableRedisIndexedHttpSession注解的@EnableRedisWebSession属性中设置值来指定namespace:
@Configuration
@EnableRedisHttpSession(redisNamespace = "spring:session:myapplication")
public class SessionConfig {
// ...
}
@Configuration
@EnableRedisIndexedHttpSession(redisNamespace = "spring:session:myapplication")
public class SessionConfig {
// ...
}
@Configuration
@EnableRedisWebSession(redisNamespace = "spring:session:myapplication")
public class SessionConfig {
// ...
}
选择RedisSessionRepository和RedisIndexedSessionRepository
当使用 Spring Session Redis 时,您可能会需要在 RedisSessionRepository 和 RedisIndexedSessionRepository 之间进行选择。
两者都是实现 SessionRepository 接口的类,用于将会话数据存储在 Redis 中。
然而,它们在处理会话索引和查询方面有所不同。
-
RedisSessionRepository:RedisSessionRepository是一个基本实现,它将会话数据存储在 Redis 中而不需要任何额外的索引。 它使用简单的键值结构来存储会话属性。 每个会话都会被分配一个唯一的会话 ID,并且会话数据存储在一个与该 ID 相关联的 Redis 键下。 当需要检索会话时,仓库会通过会话 ID 查询 Redis 以获取相关的会话数据。 由于没有索引,在基于会话属性或除会话 ID 外的其他条件查询会话时可能会效率低下。 -
RedisIndexedSessionRepository:RedisIndexedSessionRepository是一个扩展实现,提供了将会话存储在 Redis 中的功能。它在 Redis 中引入了额外的数据结构,以高效地根据属性或条件查询会话。 除了RedisSessionRepository使用的关键值结构外,它还维护了额外的索引以实现快速查找。例如,它可以基于会话属性(如用户 ID 或最后访问时间)创建索引。 这些索引允许根据特定条件高效地查询会话,从而提高性能并启用高级会话管理功能。 此外,RedisIndexedSessionRepository还支持会话过期和删除。
当使用 RedisIndexedSessionRepository 与 Redis Cluster 配合时,您必须意识到它仅订阅集群中一个随机 Redis 节点的事件,
这可能导致某些会话索引在事件发生在其他节点时不被清理。 |
配置RedisSessionRepository
监听会话事件
经常情况下,响应会话事件是非常有价值的。例如,你可能希望根据会话生命周期执行某种类型的处理。 为了能够做到这一点,你必须使用 索引仓库。 如果你不了解索引和默认仓库之间的区别,你可以去 这个部分 查看。
在配置了索引仓库后,您现在可以开始监听SessionCreatedEvent、SessionDeletedEvent、SessionDestroyedEvent和SessionExpiredEvent事件。
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
}
}
查找特定用户的所有会话
通过检索特定用户的全部会话,您可以追踪用户在不同设备或浏览器上的活跃会话。 例如,您可以用这些信息来用于会话管理目的,比如允许用户从特定的会话中作废或注销,或者根据用户的会话活动执行某些操作。
要这样做,首先你必须使用索引仓库(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);
}
}
在上面的示例中,您可以使用getSessions方法查找特定用户的全部会话,并使用removeSession方法移除特定用户的某个会话。
配置 Redis 会话映射器
Spring Session Redis 从 Redis 中检索会话信息并将其存储在 Map<String, Object>。
这个映射需要经过一个转换过程,变成 MapSession 对象,然后在 RedisSession 中使用。
默认用于此目的的映射器被称为RedisSessionMapper。
如果会话地图中不包含构建会话所需的最小必要键,例如creationTime,该映射器将抛出异常。
缺少必需键的一种可能情况是在保存过程进行时会话密钥被并发删除,通常是因为过期。
这种情况发生的原因是使用了
如果您想自定义映射过程,可以创建BiFunction<String, Map<String, Object>, MapSession>的实现并将其设置到会话存储中。以下示例展示了如何将映射过程委托给默认映射器,但如果抛出异常,则会从 Redis 中删除会话。
-
RedisSessionRepository
-
RedisIndexedSessionRepository
-
ReactiveRedisSessionRepository
@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;
}
}
}
}
@Configuration
@EnableRedisIndexedHttpSession
public class SessionConfig {
@Bean
SessionRepositoryCustomizer<RedisIndexedSessionRepository> redisSessionRepositoryCustomizer() {
return (redisSessionRepository) -> redisSessionRepository.setRedisSessionMapper(
new SafeRedisSessionMapper(redisSessionRepository.getSessionRedisOperations()));
}
static class SafeRedisSessionMapper implements BiFunction<String, Map<String, Object>, MapSession> {
private final RedisSessionMapper delegate = new RedisSessionMapper();
private final RedisOperations<String, Object> redisOperations;
SafeRedisSessionMapper(RedisOperations<String, Object> redisOperations) {
this.redisOperations = redisOperations;
}
@Override
public MapSession apply(String sessionId, Map<String, Object> map) {
try {
return this.delegate.apply(sessionId, map);
}
catch (IllegalStateException ex) {
// if you use a different redis namespace, change the key accordingly
this.redisOperations.delete("spring:session:sessions:" + sessionId); // we do not invoke RedisIndexedSessionRepository#deleteById to avoid an infinite loop because the method also invokes this mapper
return null;
}
}
}
}
@Configuration
@EnableRedisWebSession
public class SessionConfig {
@Bean
ReactiveSessionRepositoryCustomizer<ReactiveRedisSessionRepository> redisSessionRepositoryCustomizer() {
return (redisSessionRepository) -> redisSessionRepository
.setRedisSessionMapper(new SafeRedisSessionMapper(redisSessionRepository));
}
static class SafeRedisSessionMapper implements BiFunction<String, Map<String, Object>, Mono<MapSession>> {
private final RedisSessionMapper delegate = new RedisSessionMapper();
private final ReactiveRedisSessionRepository sessionRepository;
SafeRedisSessionMapper(ReactiveRedisSessionRepository sessionRepository) {
this.sessionRepository = sessionRepository;
}
@Override
public Mono<MapSession> apply(String sessionId, Map<String, Object> map) {
return Mono.fromSupplier(() -> this.delegate.apply(sessionId, map))
.onErrorResume(IllegalStateException.class,
(ex) -> this.sessionRepository.deleteById(sessionId).then(Mono.empty()));
}
}
}
自定义会话过期存储
由于Redis的特性,如果键未被访问,在键过期后不会保证何时会触发过期事件。 如需更多详细信息,请参阅Redis文档 关于键过期。
为减轻已过期事件的不确定性,会话还会存储其预计的过期时间。
这确保了每个键在预期过期时可以被访问。
RedisSessionExpirationStore接口定义了跟踪会话及其过期时间的常见操作,并提供了一种清理已过期会话的策略。
默认情况下,每个会话过期都会跟踪到最近的一分钟。 这使得后台任务可以访问可能已过期的会话,以确保在更确定的方式中触发 Redis 过期事件。
例如:
SADD spring:session:expirations:1439245080000 expires:33fdd1b6-b496-4b33-9f7d-df96679d32fe
EXPIRE spring:session:expirations:1439245080000 2100
该后台任务将使用这些映射来显式请求每个会话过期密钥。 通过访问密钥而不是删除它,我们确保只有在TTL过期时Redis才会为我们删除该密钥。
通过自定义会话过期存储,您可以根据需要更有效地管理会话过期。
要实现这一点,您应该提供一个类型为RedisSessionExpirationStore的bean,Spring Session Data Redis 配置将会使用它:
-
SessionConfig
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);
}
}
在上方的代码中,使用了SortedSetRedisSessionExpirationStore实现方式,它使用一个有序集合来存储会话ID及其过期时间作为分数。
|
我们不会显式地删除键,因为在某些情况下,可能会错误地将一个未过期的键识别为已过期。 除非使用分布式锁(这会严重影响性能),否则无法确保过期映射的一致性。 通过简单地访问键,我们可以确保只有在该键的 TTL 过期时才会移除该键。 不过,在您的实现中您可以选择最适合的策略。 |