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

Bra*_*lor 11 spring spring-security spring-boot spring-session

我在我的应用程序中使用Spring Security和Spring Session(v1.3.1).

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

SpringSessionBackedSessionRegistry的构造函数如下:

SpringSessionBackedSessionRegistry(FindByIndexNameSessionRepository<ExpiringSession> sessionRepository) 
Run Code Online (Sandbox Code Playgroud)

Redis存储库RedisOperationsSessionRepository实现:

FindByIndexNameSessionRepository<org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession>
Run Code Online (Sandbox Code Playgroud)

那么,如何能我构建的一个实例SpringSessionBackedSessionRegistry给出的实例RedisOperationsSessionRepository

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

SpringSessionBackedSessionRegistry(FindByIndexNameSessionRepository<? extends ExpiringSession> sessionRepository) 
Run Code Online (Sandbox Code Playgroud)

Ved*_*vic 8

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

我打开PR来解决这个问题,你可以在这里跟踪它.

同时,您可以FindByIndexNameSessionRepository在配置中使用raw 进行配置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);
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 我仍然收到错误“考虑在您的配置中定义一个 'org.springframework.session.FindByIndexNameSessionRepository' 类型的 bean。” (3认同)