没有类型为'org.springframework.security.authentication.AuthenticationProvider'的合格Bean

时间:2018-12-16 07:56:49

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

我应该如何解决此错误?

SecurityConfig

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();
}


}

WebConfig

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;
    }
}

错误

  

org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为“ securityConfig”的bean时出错:不满意的依赖关系   通过方法'setAuthenticationProvider'参数0表示;   嵌套异常为   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   类型的合格豆   'org.springframework.security.authentication.AuthenticationProvider'   可用:至少有1个符合自动装配条件的bean   候选人。依赖项注释:{}

谢谢! :)

1 个答案:

答案 0 :(得分:0)

您需要在@Qualifier("daoAuthenticationProvider") bean定义上添加@Bean public DaoAuthenticationProvider authProvider(UserDetailsService userDetailsService)

或者,如果您只有一个提供者,则从@Qualifier类中删除SecurityConfig