对于最新的稳定版本,请使用 Spring Session 3.4.2! |
Redis 配置
现在您已经配置了应用程序,您可能希望开始自定义内容:
-
我想要使用 Spring Boot 属性自定义 Redis 配置
-
我需要帮助进行选择
RedisSessionRepository
或RedisIndexedSessionRepository
. -
我想指定一个不同的命名空间。
-
我想知道会话何时创建、删除、销毁或过期。
使用 JSON 序列化 Session
默认情况下,Spring Session 使用 Java 序列化来序列化会话属性。
有时可能会出现问题,尤其是当您有多个应用程序使用同一个 Redis 实例但具有同一类的不同版本时。
您可以提供RedisSerializer
bean 来自定义如何将会话序列化为 Redis。
Spring Data Redis 提供了GenericJackson2JsonRedisSerializer
它使用 Jackson 的ObjectMapper
.
@Configuration
public class SessionConfig implements BeanClassLoaderAware {
private ClassLoader loader;
@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 并按如下方式使用它:
@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer(ObjectMapper objectMapper) {
return new GenericJackson2JsonRedisSerializer(objectMapper);
}
指定不同的命名空间
多个应用程序使用同一个 Redis 实例的情况并不少见。
因此,Spring Session 使用namespace
(默认为spring:session
) 以将会话数据分开。
使用 Spring Boot 属性
您可以通过设置spring.session.redis.namespace
财产。
spring.session.redis.namespace=spring:session:myapplication
spring:
session:
redis:
namespace: "spring:session:myapplication"
使用 Annotation 的属性
您可以指定namespace
通过设置redisNamespace
属性在@EnableRedisHttpSession
,@EnableRedisIndexedHttpSession
或@EnableRedisWebSession
附注:
@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
还支持会话过期和删除。
配置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
}
}
查找特定用户的所有会话
通过检索特定用户的所有会话,您可以跨设备或浏览器跟踪用户的活动会话。 例如,您可以使用此信息进行会话管理,例如允许用户使特定会话无效或注销,或者根据用户的会话活动执行作。
为此,首先您必须使用索引存储库,然后您可以注入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
方法删除用户的特定会话。