该版本仍在开发中,尚未被视为稳定。最新稳定版请使用Spring Session 3.5.3spring-doc.cadn.net.cn

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

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

指定不同的命名空间

使用同一个Redis实例的多个应用程序并不罕见。 因此,春季课程使用了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