Spring boot 2.6.0 创建名为“webSecurityConfig”的 bean 时出错

Sur*_*ran 8 java spring-security spring-boot

我无法将我的 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)
Run Code Online (Sandbox Code Playgroud)

从 spring-boot 2.6.0 发行说明中可以清楚地看出,循环引用被禁用。并且可以通过属性重新启用它spring.main.allow-circular-references = true。但我想首先修复循环引用。谁能帮我解决这个问题?请找到下面的WebSecurityConfig类,

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)
Run Code Online (Sandbox Code Playgroud)

我的代码是开源的,可以在https://github.com/surajcm/Poseidon找到,我将尝试自行排除故障,如果能够解决此问题,我将分享更新

Hen*_*ing 17

问题是密码编码器。需要构建UserDetailsService您在类的构造函数中注入的自动配置。

您可以通过创建 bean 工厂方法来打破循环static

@Bean
public static BCryptPasswordEncoder bcryptPasswordEncoder() {
    return new BCryptPasswordEncoder();
}
Run Code Online (Sandbox Code Playgroud)

您还可以将工厂方法移动到不同的配置类。但在我看来,您WebSecurityConfig是该方法的规范位置。