【问题标题】:Spring boot 2.6.0 Error creating bean with name 'webSecurityConfig'Spring boot 2.6.0 创建名为“webSecurityConfig”的 bean 时出错
【发布时间】: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


【解决方案1】:

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

可以通过制作bean工厂方法static来打破循环:

@Bean
public static BCryptPasswordEncoder bcryptPasswordEncoder() {
    return new BCryptPasswordEncoder();
}

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

【讨论】:

猜你喜欢
  • 2021-09-21
  • 1970-01-01
  • 2023-02-09
  • 2019-02-12
  • 2020-06-05
  • 1970-01-01
  • 1970-01-01
  • 2015-07-28
  • 2018-01-25
相关资源
最近更新 更多