FindByIndexNameSessionRepository 在 Spring Boot 中不能为 null

FindByIndexNameSessionRepository cannot be null in Spring Boot

我正在尝试为我的 Spring 安全应用程序实现记住功能。我也用过Redis。我配置了 SpringSessionBackedSessionRegistry 个 bean。但是当我尝试 @Autowired FindByIndexNameSessionRepository 时出现以下错误,

Caused by: java.lang.IllegalArgumentException: sessionRepository cannot be null

代码如下,

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private FindByIndexNameSessionRepository<? extends Session> sessionRepository;
    
    @Bean
    public SpringSessionBackedSessionRegistry sessionRegistry() {
        return new SpringSessionBackedSessionRegistry(this.sessionRepository);
    }
}

Handler.java

@Component
public class Handler {

    private @Autowired SessionRegistry sessionRegistry;
    private @Autowired FindByIndexNameSessionRepository<? extends Session> sessionRepository;
    
}

和 Redis 会话配置,

@Configuration
@EnableSpringHttpSession
public class SessionConfig {

    private final RedisConnectionFactory redisConnectionFactory;

    public SessionConfig(ObjectProvider<RedisConnectionFactory> redisConnectionFactory) {
        this.redisConnectionFactory = redisConnectionFactory.getIfAvailable();
    }
    
    @Bean
    public RedisOperations<String, Object> sessionRedisOperations() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(this.redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }

    @Bean
    public RedisSessionRepository redisSessionRepository(RedisOperations<String, Object> sessionRedisOperations) {
        return new RedisSessionRepository(sessionRedisOperations);
    }
}

我收到类似

的错误

Caused by: java.lang.IllegalArgumentException: sessionRepository cannot be null Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.session.security.SpringSessionBackedSessionRegistry]: Circular reference involving containing bean 'securityConfig' - consider declaring the factory method as static for independence from its containing instance. Factory method 'sessionRegistry' threw exception; nested exception is java.lang.IllegalArgumentException: sessionRepository cannot be null Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionRegistry' defined in class path resource [SecurityConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.session.security.SpringSessionBackedSessionRegistry]: Circular reference involving containing bean 'securityConfig' - consider declaring the factory method as static for independence from its containing instance. Factory method 'sessionRegistry' threw exception; nested exception is java.lang.IllegalArgumentException: sessionRepository cannot be null

还说,

No beans of 'FindByIndexNameSessionRepository<? extends Session>' type found.

那么我做错了什么?

也许,我不确定,因为这个循环依赖错误...

无论如何,问题可能正在发生,因为您实际上并没有在 Spring 上下文中注入 FindByIndexNameSessionRepository,至少不是您尝试配置的那个,而是 SessionRepository,因为您定义的是 RedisSessionRepository and this interface implements SessionRepository, not FindByIndexNameSessionRepository. Please, try to use a RedisIndexedSessionRepository 类型的 bean,类似于:

@Configuration
@EnableSpringHttpSession
public class SessionConfig {

    private final RedisConnectionFactory redisConnectionFactory;

    public SessionConfig(ObjectProvider<RedisConnectionFactory> redisConnectionFactory) {
        this.redisConnectionFactory = redisConnectionFactory.getIfAvailable();
    }
    
    @Bean
    public RedisOperations<String, Object> sessionRedisOperations() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(this.redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }

    @Bean
    public FindByIndexNameSessionRepository redisSessionRepository(RedisOperations<String, Object> sessionRedisOperations) {
        return new RedisIndexedSessionRepository(sessionRedisOperations);
    }
}