亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有多個“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 關注
  • 300 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號