【问题标题】:Error creating bean with name 'securityConfig': Requested bean is currently in creation:创建名为“securityConfig”的 bean 时出错:请求的 bean 当前正在创建中:
【发布时间】:2022-01-04 15:52:57
【问题描述】:
package ro.contabilitateexpert.AccountExpert.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.BeanIds;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

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

    @Override
    public void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.csrf().disable().authorizeRequests()
                .antMatchers("/api/auth/**")
                .permitAll()
                .anyRequest()
                .authenticated();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean
    PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

我有以下错误: 创建名为“securityConfig”的 bean 时出错:当前正在创建请求的 bean:是否存在无法解析的循环引用?

我该如何解决?

【问题讨论】:

  • 嗯...是否存在无法解析的循环引用?
  • 需要从我的项目中添加更多代码来清除它吗?
  • 不,只有一点点澄清:为什么您的@Configuration 上没有@Configuration ..适配器?
  • 不,不是,我正在努力学习spring,所以如果你能帮助我提供建议会很棒
  • 谢谢@AlexeyVeleshko 成功了

标签: java spring spring-boot hibernate spring-mvc


【解决方案1】:

在我使用 @Overrides 更改为 configure 而不是 configureGlobal 并删除 @Autowired 之后 添加了@Configuration 现在代码正在运行,

感谢 Alexey Veleshko

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
  • 相当无用的答案,因为人们无法理解或复制您所做的事情。
  • 你能把代码也发布出来吗?
【解决方案2】:

更详细的解决方案:

之前的代码:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
    authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}

更改@Autowired 和函数名称

@Override
public void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
    authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
}

【讨论】:

    猜你喜欢
    • 2019-06-15
    • 2020-03-24
    • 2021-11-21
    • 2017-11-26
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 2021-11-28
    相关资源
    最近更新 更多