现在,您已经配置了应用程序,您可能希望开始自定义内容:

使用 JSON 序列化会话

默认情况下,Spring Session 使用 Java 序列化来序列化会话属性。 有时可能会出现问题,尤其是当您有多个应用程序使用相同的 Redis 实例但具有同一类的不同版本时。 您可以提供一个 Bean 来自定义会话序列化到 Redis 的方式。 Spring Data Redis 提供了使用 Jackson 的 .RedisSerializerGenericJackson2JsonRedisSerializerObjectMapper

配置 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 模块,你可以注入应用程序的 bean 并像这样使用它:ObjectMapperObjectMapper

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

指定其他命名空间

使用同一 Redis 实例的多个应用程序并不少见。 出于这个原因,Spring Session 使用(默认为 )来保持会话数据的分离(如果需要)。namespacespring:session

使用 Spring Boot 属性

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

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

使用注释的属性

您可以通过在 、 或 注释中设置属性来指定:namespaceredisNamespace@EnableRedisHttpSession@EnableRedisIndexedHttpSession@EnableRedisWebSession

@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 {
    // ...
}

在 和 之间进行选择RedisSessionRepositoryRedisIndexedSessionRepository

使用 Spring Session Redis 时,您可能必须在 和 之间进行选择。 两者都是在 Redis 中存储会话数据的接口的实现。 但是,它们在处理会话索引和查询的方式上有所不同。RedisSessionRepositoryRedisIndexedSessionRepositorySessionRepository

  • RedisSessionRepository:是一种基本实现,无需任何额外的索引即可将会话数据存储在 Redis 中。 它使用简单的键值结构来存储会话属性。 每个会话都分配有一个唯一的会话 ID,会话数据存储在与该 ID 关联的 Redis 密钥下。 当需要检索会话时,存储库会使用会话 ID 查询 Redis 以获取关联的会话数据。 由于没有索引,因此基于会话 ID 以外的属性或条件查询会话可能效率低下。RedisSessionRepository

  • RedisIndexedSessionRepository:是一种扩展实现,可为存储在 Redis 中的会话提供索引功能。 它在 Redis 中引入了额外的数据结构,以根据属性或条件高效查询会话。 除了 使用的键值结构之外,它还维护其他索引以实现快速查找。 例如,它可以根据会话属性(如用户 ID 或上次访问时间)创建索引。 这些索引允许根据特定条件对会话进行高效查询,从而增强性能并启用高级会话管理功能。 除此之外,还支持会话过期和删除。RedisIndexedSessionRepositoryRedisSessionRepositoryRedisIndexedSessionRepository

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

配置RedisSessionRepository

使用 Spring Boot 属性

如果您使用的是 Spring Boot,则 是默认实现。 但是,如果要明确说明这一点,可以在应用程序中设置以下属性:RedisSessionRepository

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

使用批注

您可以使用以下注解进行配置:RedisSessionRepository@EnableRedisHttpSession

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

配置RedisIndexedSessionRepository

使用 Spring Boot 属性

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

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

使用批注

您可以使用以下注解进行配置:RedisIndexedSessionRepository@EnableRedisIndexedHttpSession

@Configuration
@EnableRedisIndexedHttpSession
public class SessionConfig {
    // ...
}
与 Redis 集群一起使用时,您必须注意,它只订阅集群中一个随机 Redis 节点的事件,如果事件发生在其他节点中,这可能会导致某些会话索引无法清理。RedisIndexedSessionRepository

收听会话事件

通常,对会话事件做出反应很有价值,例如,您可能希望根据会话生命周期执行某种处理。 为了能够做到这一点,您必须使用索引存储库。 如果您不知道索引存储库和默认存储库之间的区别,可以转到此部分

配置索引存储库后,您现在可以开始侦听 、 和 事件。 在 Spring 中,有几种方法可以监听应用程序事件,我们将使用注解。SessionCreatedEventSessionDeletedEventSessionDestroyedEventSessionExpiredEvent@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);
    }
}

在上面的示例中,您可以使用该方法来查找特定用户的所有会话,以及删除用户的特定会话的方法。getSessionsremoveSession

配置 Redis 会话映射器

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

用于此目的的默认映射器称为 。 如果会话映射不包含构造会话所需的最小键,例如 ,此映射器将引发异常。 缺少所需密钥的一种可能情况是,在保存过程正在进行时,会话密钥同时被删除(通常是由于过期)。 发生这种情况的原因是使用 HSET 命令在键中设置字段,如果该键不存在,则此命令将创建它。RedisSessionMappercreationTime

如果要自定义映射过程,可以创建实现并将其设置到会话存储库中。 以下示例演示如何将映射过程委托给默认映射器,但如果引发异常,则会从 Redis 中删除会话:BiFunction<String, Map<String, Object>, MapSession>

  • 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()));
        }

    }

}