Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
86 views
in Technique[技术] by (71.8m points)

java - use session-data-redis @Autowired FindByIndexNameSessionRepository run error

this is my pom,xml

       <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
            <version>2.2.7.RELEASE</version>
    </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

this is my SecurityConfig

@Configuration
public class WebSecurityConfig<S extends Session> extends WebSecurityConfigurerAdapter {
private final String successForwardUrl = "/auth/session";
    private final String failureForwardUrl = "/auth/loginFail";

    @Autowired
    private MobileCodeSecurityConfigurer mobileCodeSecurityConfigurer;

    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(mobileCodeAuthenticationProvider())
            .authenticationProvider(usernamePasswordAuthenticationProvider());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors();
        http.headers().frameOptions().disable();
        http.csrf().disable().exceptionHandling()
            .authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED));

        List<String> authAntPatterns = new ArrayList<>();
        authAntPatterns.add("/auth/**");
        String[] authAntPatternsValue = authAntPatterns.toArray(new String[authAntPatterns.size()]);
        http.authorizeRequests().antMatchers(authAntPatternsValue).permitAll();
        http.authorizeRequests().anyRequest().authenticated();

        http.formLogin().loginProcessingUrl("/auth/login").successHandler(authenticationSuccessHandler())
            .failureHandler(authenticationFailureHandler());

        http.logout().invalidateHttpSession(true).logoutUrl("/auth/logout")
            .logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler());

        http.sessionManagement().maximumSessions(1).maxSessionsPreventsLogin(false).sessionRegistry(sessionRegistry());

        http.apply(mobileCodeSecurityConfigurer);
    }

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

    @Autowired
    private FindByIndexNameSessionRepository<S> sessionRepository;

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

this is my RedisSessionConfig

@EnableRedisHttpSession
public class RedisHttpSessionConfig {

    @Bean
    public LettuceConnectionFactory connectionFactory() {
        RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration("182.43.172.163", 6379);
        return new LettuceConnectionFactory(standaloneConfig);
    }
}

this is error msg。What should I do with it !!!

Error creating bean with name 'sessionRepositoryFilterRegistration' defined in class path resource [org/springframework/boot/autoconfigure/session/SessionRepositoryFilterConfiguration.class]: Unsatisfied dependency expressed through method 'sessionRepositoryFilterRegistration' parameter 1; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration': Unsatisfied dependency expressed through method 'setHttpSessionListeners' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig': Unsatisfied dependency expressed through field 'sessionRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionRepository' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.session.data.redis.RedisIndexedSessionRepository]: Circular reference involving containing bean 'org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration' - consider declaring the factory method as static for independence from its containing instance. Factory method 'sessionRepository' threw exception; nested exception is java.lang.IllegalStateException: RedisConnectionFactory is required
question from:https://stackoverflow.com/questions/65952104/use-session-data-redis-autowired-findbyindexnamesessionrepository-run-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should declare the HttpSessionEventPublisher bean in a different @Configuration class than the one where you inject FindByIndexNameSessionRepository.

Declaring HttpSessionEventPublisher bean in the same @Configuration class where you inject FindByIndexNameSessionRepository, creates a cycle since SpringHttpSessionConfiguration injects all HttpSessionListener beans.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...