FindByIndexNameSessionRepository “需要RedisConnectionFactory”配置错误

FindByIndexNameSessionRepository “RedisConnectionFactory is required” configuration error

实现限制每个用户活动会话的逻辑(存储在数据库中的可能会话数,Redis 中的会话数),我遇到了配置问题FindByIndexNameSessionRepository

由于我正在注入 FindByIndexNameSessionRepository spring 开始配置 RedisHttpSessionConfiguration class 中的 bean,并且字段未注入 class 通过设置器。

结果,我得到错误:

Caused by: java.lang.IllegalStateException: RedisConnectionFactory is required
at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.data.redis.core.RedisAccessor.afterPropertiesSet(RedisAccessor.java:38) ~[spring-data-redis-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.data.redis.core.RedisTemplate.afterPropertiesSet(RedisTemplate.java:127) ~[spring-data-redis-2.2.6.RELEASE.jar:2.2.6.RELEASE]
at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration.createRedisTemplate(RedisHttpSessionConfiguration.java:291) ~[spring-session-data-redis-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration.sessionRepository(RedisHttpSessionConfiguration.java:120) ~[spring-session-data-redis-2.2.2.RELEASE.jar:2.2.2.RELEASE]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]

配置代码:

@EnableRedisHttpSession
@EnableWebSecurity
@Configuration
@RequiredArgsConstructor
public class CustomConfiguration extends WebSecurityConfigurerAdapter {
    private final FindByIndexNameSessionRepository sessionRepository;    
    // ....

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/**")
                .authenticated()
                .and()
            .formLogin()
                .and()
            // ....
            .sessionManagement()
                .sessionAuthenticationStrategy(new CustomAuthenticationStrategy(sessionRegistry(), sessionsLimitRepository));
    }

    @Bean
    public SpringSessionBackedSessionRegistry sessionRegistry() {
        return new SpringSessionBackedSessionRegistry(sessionRepository);
    }

    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory(new RedisStandaloneConfiguration("server", 6379));
    }

    @Bean
    public HttpSessionEventPublisher httpSessionEventPublisher() {
        return new HttpSessionEventPublisher();
   }

}

pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-core</artifactId>
    </dependency>

    <!-- .... -->
</dependencies>

如果你 remove\comment 排除了使用 FindByIndexNameSessionRepository 的地方,那么当 spring 的 "guts" 调用 RedisHttpSessionConfiguration springSessionRepositoryFilter bean 已创建。

如果我没理解错的话,那我不小心打乱了配置bean的顺序。为了修复它,我尝试了@DependsOn、@Lazy 和各种欺诈行为,将 bin 移动到不同的 classes 配置,更改加载配置的顺序,等等。

UPD:添加 bean httpSessionEventPublisher 和 pom.xml

主要问题是 spring-boot 已经为我配置了大部分垃圾箱。

要解决这个问题,你必须删除@EnableRedisHttpSession@EnableWebSecurity也可以删除),bean redisConnectionFactoryhttpSessionEventPublisher,并且可以选择删除不需要的依赖性 spring-session-core

实际配置代码:

@Configuration
@RequiredArgsConstructor
public class CustomConfiguration extends WebSecurityConfigurerAdapter {

    private final FindByIndexNameSessionRepository<? extends Session> sessionRepository;

    // ....

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/**")
                .authenticated()
                .and()
            .formLogin()
                .and()
            // ....
            .sessionManagement()
                .sessionAuthenticationStrategy(new CustomAuthenticationStrategy(sessionRegistry(), sessionsLimitRepository));
}

    @Bean
    public SessionRegistry sessionRegistry() {
        return new SpringSessionBackedSessionRegistry<>(sessionRepository);
    }

}

实际pom.xml

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-data-redis</artifactId>
    </dependency>

    <!-- .... -->
</dependencies>