当前位置: 首页 > 知识库问答 >
问题:

没有“org”类型的合格bean。springframework。安全认证。AuthenticationProvider'

栾瑞
2023-03-14

我应该如何修复这个错误?

证券配置

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;



@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {


private AuthenticationProvider authenticationProvider;

@Autowired
@Qualifier("daoAuthenticationProvider")
public void setAuthenticationProvider(AuthenticationProvider authenticationProvider) {
    this.authenticationProvider = authenticationProvider;
}

@Bean
public PasswordEncoder encoder() {
    return new BCryptPasswordEncoder(11);
}

@Bean
public DaoAuthenticationProvider authProvider(UserDetailsService userDetailsService) {
    final DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
    authProvider.setUserDetailsService(userDetailsService);
    authProvider.setPasswordEncoder(encoder());
    return authProvider;
}

@Autowired
public void configureAuthManager(AuthenticationManagerBuilder authenticationManagerBuilder){
    authenticationManagerBuilder.authenticationProvider(authenticationProvider);
}    

@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
       httpSecurity
            .authorizeRequests().antMatchers("/","/products","/product/show/*","/console/*","/h2-console/**").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin().loginPage("/login").permitAll()
            .and()
            .logout().permitAll();

    httpSecurity.csrf().disable();
    httpSecurity.headers().frameOptions().disable();
}


}

网络配置

import org.h2.server.web.WebServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;`
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WebConfig {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Bean
    ServletRegistrationBean H2ServletRegstration() {
        ServletRegistrationBean registrationBean = new  ServletRegistrationBean(new WebServlet());
        registrationBean.addUrlMappings("/console/*");
        return registrationBean;
    }
}

错误

组织。springframework。豆。工厂UnsatifiedPendencyException:创建名为“securityConfig”的bean时出错:通过方法“setAuthenticationProvider”参数0表示的未满足的依赖关系;嵌套的异常是org。springframework。豆。工厂NoSuchBean定义异常:没有“org”类型的合格bean。springframework。安全认证。AuthenticationProvider’available:至少需要1个符合autowire候选资格的bean。依赖项注释:{}

谢谢!:)

共有1个答案

邓俊材
2023-03-14

您需要在您的@Bean公共DaoAuthentiationProvider(UserDetailsService userDetailsService)上添加@限定符("daoAuthentiationProvider")bean定义。

或者从SecurityConfig类中删除@Qualiator,如果您有单个提供程序。

 类似资料: