Redis 配置

使用 JSON 序列化会话

默认情况下,Spring Session 使用 Java 序列化来序列化会话属性。有时可能会有问题,尤其是当你有多个应用程序使用同一个 Redis 实例但同一类的不同版本时。你可以提供重写序列器Bean 可以自定义会话如何序列化到 Redis。 Spring Data Redis提供了GenericJackson2JsonRedisSerializer利用 Jackson 的对象映射器.spring-doc.cadn.net.cn

配置 RedisSerializer
@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,因此我们创建了一个自定义代码对象映射器该模块使用了Spring Security的Jackson模块。 如果你不需要Spring Security Jackson模块,可以注入你的应用程序对象映射器然后像这样用:spring-doc.cadn.net.cn

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

指定不同的命名空间

多个应用程序使用同一个 Redis 实例的情况并不罕见。因此,Spring Session 使用Namespace(默认为春季:会期)以便在需要时保持会话数据分离。spring-doc.cadn.net.cn

使用 Spring Boot 属性

你可以通过设置spring.session.redis.namespace财产。spring-doc.cadn.net.cn

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

使用注释的属性

你可以指定Namespace通过设置redisNamespace财产在@EnableRedisHttpSession,@EnableRedisIndexedHttpSession@EnableRedisWebSession附注:spring-doc.cadn.net.cn

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

选择RedisSession仓库RedisIndexedSessionRepository

使用春季课程Redis时,你很可能需要在RedisSession仓库以及RedisIndexedSessionRepository. 两者都是会话仓库在 Redis 中存储会话数据的接口。然而,它们在处理会话索引和查询的方式上有所不同。spring-doc.cadn.net.cn

  • RedisSession仓库:RedisSession仓库是一个基本实现,无需额外索引即可在 Redis 中存储会话数据。它使用简单的键值结构来存储会话属性。每个会话都被分配了唯一的会话 ID,会话数据存储在与该 ID 关联的 Redis 键下。当需要检索会话时,仓库会用会话 ID 查询 Redis 获取相关的会话数据。由于没有索引,基于除会话 ID 以外的属性或条件查询会话可能效率较低。spring-doc.cadn.net.cn

  • RedisIndexedSessionRepository:RedisIndexedSessionRepository是一个扩展实现,为存储在 Redis 中的会话提供索引功能。它在 Redis 中引入了额外的数据结构,以高效地基于属性或条件查询会话。除了RedisSession仓库它维护额外的索引以实现快速查找。例如,它可以基于会话属性(如用户ID或最后访问时间)创建索引。这些索引允许基于特定条件高效查询会话,提升性能并支持高级会话管理功能。此外,RedisIndexedSessionRepository同时支持会话过期和删除。spring-doc.cadn.net.cn

使用RedisIndexedSessionRepository使用 Redis 集群时,你必须注意它只订阅来自集群中某个随机 Redis 节点的事件,这可能导致某些会话索引无法清理,如果事件发生在不同的节点。

配置RedisSession仓库

使用 Spring Boot 属性

如果你用的是Spring Boot,RedisSession仓库是默认实现。不过,如果你想明确说明,可以在应用中设置以下属性:spring-doc.cadn.net.cn

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

使用注释

你可以配置RedisSession仓库通过使用@EnableRedisHttpSession注解:spring-doc.cadn.net.cn

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

配置RedisIndexedSessionRepository

使用 Spring Boot 属性

你可以配置RedisIndexedSessionRepository通过在你的申请中设置以下属性:spring-doc.cadn.net.cn

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

使用注释

你可以配置RedisIndexedSessionRepository通过使用@EnableRedisIndexedHttpSession注解:spring-doc.cadn.net.cn

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

聆听会议活动

通常,响应会话事件很有价值,例如,根据会话生命周期,你可能想做某种处理。要做到这一点,你必须使用索引仓库。如果你不清楚索引仓库和默认仓库的区别,可以访问本节spring-doc.cadn.net.cn

配置好索引仓库后,你现在可以开始收听SessionCreatedEvent,SessionDeletedEvent,SessionDestroyedEventSessionExpiredEvent事件。 春季有几种方式可以收听申请事件,我们将使用@EventListener注解。spring-doc.cadn.net.cn

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

}

查找特定用户的所有会话

通过检索特定用户的所有会话,你可以追踪该用户在不同设备或浏览器中的活跃会话。例如,你可以利用这些信息会话管理功能,比如允许用户取消或登出特定会话,或根据用户会话活动执行作。spring-doc.cadn.net.cn

要做到这一点,首先你必须使用索引仓库,然后你可以注入FindByIndexNameSessionRepository界面,像这样:spring-doc.cadn.net.cn

@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移除用户特定会话的方法。spring-doc.cadn.net.cn

配置Redis会话映射器

春季会话 Redis 从 Redis 获取会话信息并存储在Map<String,对象>. 该映射需要经过映射过程,才能转化为地图会话对象,然后在再会.spring-doc.cadn.net.cn

用于此目的的默认映射器称为RedisSessionMapper. 如果会话映射不包含构建会话所需的最小键,比如创作时间该映射器会抛出异常。缺少所需密钥的一种可能情况是会话密钥同时被删除,通常是因为过期,而保存过程正在进行中。这是因为使用HSET命令在密钥内设置字段,如果密钥不存在,该命令将生成该字段。spring-doc.cadn.net.cn

如果你想自定义映射过程,可以创建你的实现双功能(BiFunction<String)、映射(Map<String)、对象(Object>)、地图会话(MapSession)>并将其设置为会话仓库。以下示例展示了如何将映射过程委托给默认映射器,但如果抛出异常,会话将从Redis中删除:spring-doc.cadn.net.cn

@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 关于密钥过期的文档。spring-doc.cadn.net.cn

为了减少事件过期的不确定性,会话也会被存储其预期的到期时间。这确保每个密钥在预期到期时都能被访问。 这RedisSessionExpirationStore界面定义了跟踪会话及其到期时间的常用作,并提供了清理过期会话的策略。spring-doc.cadn.net.cn

默认情况下,每个会话的过期时间都会被追踪到最接近的分钟。这使得后台任务能够访问可能已过期的会话,确保 Redis 过期事件以更确定的方式被触发。spring-doc.cadn.net.cn

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

后台任务会利用这些映射明确请求每个会话的密钥过期。通过访问密钥而非删除密钥,我们确保 Redis 只有在 TTL 过期时才会为我们删除密钥。spring-doc.cadn.net.cn

通过自定义会话到期存储,你可以更有效地根据需求管理会话到期。为此,你需要提供一个类型的“豆子”RedisSessionExpirationStore该配置将被春季会话数据Redis配置接收:spring-doc.cadn.net.cn

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,其过期时间作为评分。spring-doc.cadn.net.cn

我们不会明确删除密钥,因为在某些情况下可能存在竞态条件,错误地将密钥识别为过期,而实际上它并未过期。除非使用分布式锁(这会牺牲性能),否则无法确保过期映射的一致性。通过直接访问密钥,我们确保只有当该密钥的TTL过期时才会移除密钥。不过,针对你的实现,你可以选择最适合的策略。spring-doc.cadn.net.cn