为了账号安全,请及时绑定邮箱和手机立即绑定

有多个“UserDetailsS​​ervice”类型的 bean。

有多个“UserDetailsS​​ervice”类型的 bean。

慕田峪7331174 2024-01-28 17:29:24
我无法在我的类 SecurityConfig 中自动装配 UserDetailsService,并且在尝试启动应用程序时出现错误。我不知道它不起作用,我已经开始了一个具有相同配置的项目。SecurityConfig.javapackage ma.wearesport.wearesport.security;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.HttpMethod;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.builders.WebSecurity;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;@Configuration@EnableWebSecuritypublic class SecurityConfig extends WebSecurityConfigurerAdapter {  @Bean  public BCryptPasswordEncoder bCryptPasswordEncoder() {    return new BCryptPasswordEncoder();  }我尝试添加 @Qualifier("UserDetailsServiceImpl"),但它不起作用。  @Autowired  private UserDetailsService userDetailsService;  @Autowired  private BCryptPasswordEncoder passwordEncoder;  @Override  protected void configure(AuthenticationManagerBuilder auth) throws Exception {    auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);  }  @Override  public void configure(WebSecurity web) {    super.configure(web);  }  @Override  protected void configure(HttpSecurity http) throws Exception {    http.csrf().disable();    http.authorizeRequests().antMatchers(HttpMethod.POST, "api/register").permitAll();  }}
查看完整描述

2 回答

?
慕的地10843

TA贡献1785条经验 获得超8个赞

你有一个,因为你通过在定义它的 bean中BeanCurrentlyInCreationException自动装配 bean 来创建 bean 之间的循环依赖关系。BCryptPasswordEncoderSecurityConfig


这是您必须删除的内容:


@Autowired

private BCryptPasswordEncoder passwordEncoder;

相反,您应该使用方法引用来引用BCryptPasswordEncoder:


@Configuration

@EnableWebSecurity

public class SecurityConfig extends WebSecurityConfigurerAdapter {


  @Autowire

  private UserDetailsService userDetailsService;


  @Bean

  public BCryptPasswordEncoder bCryptPasswordEncoder() {

    return new BCryptPasswordEncoder();

  }


  @Override

  protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    auth.userDetailsService(userDetailsService)

        .passwordEncoder(bCryptPasswordEncoder());

  }

}

或者在不扩展的BCryptPasswordEncoder单独类中声明bean 。@ConfigurationWebSecurityConfigurerAdapter


查看完整回答
反对 回复 2024-01-28
?
临摹微笑

TA贡献1982条经验 获得超2个赞

您可以直接注入 UserDetailsServiceImpl :

@Autowired
private UserDetailsServiceImpl userDetailsService;


查看完整回答
反对 回复 2024-01-28
  • 2 回答
  • 0 关注
  • 107 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信