使用 session-data-redis @Autowired FindByIndexNameSessionRepository 运行 错误
use session-data-redis @Autowired FindByIndexNameSessionRepository run error
这是我的 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>
这是我的安全配置
@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);
}
}
这是我的 RedisSessionConfig
@EnableRedisHttpSession
public class RedisHttpSessionConfig {
@Bean
public LettuceConnectionFactory connectionFactory() {
RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration("182.43.172.163", 6379);
return new LettuceConnectionFactory(standaloneConfig);
}
}
这是错误信息。我该怎么办!!!
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
您应该在不同于注入 FindByIndexNameSessionRepository
.
的 @Configuration
class 中声明 HttpSessionEventPublisher
bean
在注入 FindByIndexNameSessionRepository
的同一 @Configuration
class 中声明 HttpSessionEventPublisher
bean,创建一个循环,因为 SpringHttpSessionConfiguration
注入所有 HttpSessionListener
豆.
这是我的 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>
这是我的安全配置
@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);
}
}
这是我的 RedisSessionConfig
@EnableRedisHttpSession
public class RedisHttpSessionConfig {
@Bean
public LettuceConnectionFactory connectionFactory() {
RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration("182.43.172.163", 6379);
return new LettuceConnectionFactory(standaloneConfig);
}
}
这是错误信息。我该怎么办!!!
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
您应该在不同于注入 FindByIndexNameSessionRepository
.
@Configuration
class 中声明 HttpSessionEventPublisher
bean
在注入 FindByIndexNameSessionRepository
的同一 @Configuration
class 中声明 HttpSessionEventPublisher
bean,创建一个循环,因为 SpringHttpSessionConfiguration
注入所有 HttpSessionListener
豆.