将 SpringSessionBackedSessionRegistry 与 Redis 会话存储库一起使用

Using SpringSessionBackedSessionRegistry with Redis Session Repository

我在我的应用程序中使用 Spring 安全和 Spring 会话 (v1.3.1)。

我想使用 SpringSessionBackedSessionRegistry 作为我的会话注册表和 Redis 作为我的会话存储库。

SpringSessionBackedSessionRegistry的构造函数如下:

SpringSessionBackedSessionRegistry(FindByIndexNameSessionRepository<ExpiringSession> sessionRepository) 

Redis 存储库,RedisOperationsSessionRepository 实现:

FindByIndexNameSessionRepository<org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession>

那么,如何在给定 RedisOperationsSessionRepository 的情况下构造 SpringSessionBackedSessionRegistry 的实例?

为什么 SpringSessionBackedSessionRegistry 的构造函数不是:

SpringSessionBackedSessionRegistry(FindByIndexNameSessionRepository<? extends ExpiringSession> sessionRepository) 

SpringSessionBackedSessionRegistry 应该将 FindByIndexNameSessionRepository<? extends ExpiringSession> sessionRepository 作为构造函数参数是正确的。

我已经打开 PR 来解决这个问题,你可以跟踪它 here

与此同时,您可以在配置中使用原始 FindByIndexNameSessionRepository 来配置 SpringSessionBackedSessionRegistry。这是一个例子:

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private FindByIndexNameSessionRepository sessionRepository;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .and()
            .sessionManagement()
                .maximumSessions(1)
                .sessionRegistry(sessionRegistry());
    }

    @Bean
    @SuppressWarnings("unchecked")
    public SpringSessionBackedSessionRegistry sessionRegistry() {
        return new SpringSessionBackedSessionRegistry(this.sessionRepository);
    }

}