【发布时间】:2022-01-12 04:43:58
【问题描述】:
我无法将我的 Spring Boot 应用程序从 2.5.7 更新到 2.6.0。它会引发以下错误。
2021-12-07T08:40:22,311 ERROR [restartedMain] o.s.b.SpringApplication.reportFailure:819|| Application run failed org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'webSecurityConfig':
The requested bean is currently in creation: Is there an unresolvable circular reference?
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.beforeSingletonCreation(DefaultSingletonBeanRegistry.java:355)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:227)
从 spring-boot 2.6.0 发行说明中,很明显循环引用被禁用。并且可以使用属性spring.main.allow-circular-references = true 重新启用它。但我想首先修复循环引用。谁能帮我解决这个问题?请在下面找到 WebSecurityConfig 类,
@Configuration
@EnableWebSecurity
@SuppressWarnings({"PMD.SignatureDeclareThrowsException"})
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
private final UserDetailsService userDetailsService;
public WebSecurityConfig(final UserDetailsService userDetailsService) {
this.userDetailsService = userDetailsService;
}
@Bean
public BCryptPasswordEncoder bcryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
protected void configure(final HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**", "/registration", "/css/**", "/js/**", "/h2-console/*").permitAll()
.anyRequest().authenticated().and()
.formLogin()
.loginPage("/login").permitAll().and()
.headers()
.frameOptions().sameOrigin().and()
.logout()
.permitAll().and()
.requiresChannel()
.requestMatchers(r -> r.getHeader("X-Forwarded-Proto") != null)
.requiresSecure();
}
@Autowired
public void configureGlobal(final AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bcryptPasswordEncoder());
}
}
我的代码是开源的,可以在https://github.com/surajcm/Poseidon 找到,我会尝试自己解决问题,如果我能够解决这个问题,我会分享更新
【问题讨论】:
-
这个类看起来很可疑,自定义安全性是不好的做法github.com/surajcm/Poseidon/blob/… 你不应该注入整个 websecurityconfig 也不需要覆盖“configureGlobal”,编码器是一个 bean,你的 userdetailsservice 是一个豆子,会自动捡起来。无需手动设置。这在官方文档中有完整的记录。
标签: java spring-boot spring-security