Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在spring boot应用程序中正确配置更多身份验证提供程序_Java_Spring_Spring Boot - Fatal编程技术网

Java 如何在spring boot应用程序中正确配置更多身份验证提供程序

Java 如何在spring boot应用程序中正确配置更多身份验证提供程序,java,spring,spring-boot,Java,Spring,Spring Boot,我的应用程序具有此安全设置: @Configuration @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) public static class ApplicationSecurity extends WebSecurityConfigurerAdapter { @Autowired private AuthService authService; @Autowired public void configu

我的应用程序具有此安全设置:

@Configuration
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
public static class ApplicationSecurity extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthService authService;

    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(customAuthenticationProvider());
        auth.authenticationProvider(daoAuthenticationProvider());
    }

    @Bean
    public CustomAuthenticationProvider customAuthenticationProvider() {
        return new CustomAuthenticationProvider();
    }

    @Bean
    public DaoAuthenticationProvider daoAuthenticationProvider() {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setUserDetailsService(authService);
        provider.setPasswordEncoder(new BCryptPasswordEncoder());
        return provider;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.requestMatcher(new BasicRequestMatcher()).antMatcher("/**").authorizeRequests().anyRequest()
                .fullyAuthenticated().and().httpBasic().and().csrf().disable();
        http.addFilterBefore(customAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    }

    @Autowired
    private AuthenticationManager authenticationManager;

    private Logger log = LoggerFactory.getLogger(ApplicationSecurity.class);

    public CustomAuthenticationFilter customAuthenticationFilter() throws Exception {
        List<AuthenticationProvider> a = ((ProviderManager) authenticationManager).getProviders();
        log.debug("providers: " + a);
        return new CustomAuthenticationFilter(authenticationManager);
    }

}
我不知道为什么有2个DaoAuthenticationProvider。当我像这样编辑配置时:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(kaasAuthenticationProvider());
    // auth.authenticationProvider(daoAuthenticationProvider());
}

@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder();
}
那就行了。只有一个DaoAuthenticationProvider。问题是,我不知道为什么现在可以这样做,所以我不想使用它,直到我了解这种安全性是如何工作的

更新:

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

    private Logger log = LoggerFactory.getLogger(CustomAuthenticationProvider.class);

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        log.debug("Authentication: {}.", authentication);
        ...
        return new CustomAuthenticationToken(securityToken, authorities,
                new CustomUser(login, "", true, true, true, true, authorities));
    }
}
好的,我发现了问题。线对我帮助很大

我是autowiring authenticationManager:

@Autowired
private AuthenticationManager authenticationManager;
在上面的线程中没有委托:

   @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
   @Override
   public AuthenticationManager authenticationManagerBean() throws Exception {
       return super.authenticationManagerBean();
   }
这导致:

class InitializeUserDetailsManagerConfigurer
        extends GlobalAuthenticationConfigurerAdapter {
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        if (auth.isConfigured()) {
            return;
        }
        ...
}
是在之前执行的

@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(customAuthenticationProvider());
    auth.authenticationProvider(daoAuthenticationProvider());
}

为什么会有一个附加的DaoAuthenticationProvider

发布您的CustomAuthenticationProvider代码。为什么需要此提供程序?这是一个复杂的,我不能粘贴代码。我只是更新了这里的骨架
@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(customAuthenticationProvider());
    auth.authenticationProvider(daoAuthenticationProvider());
}