Spring安全-如何使用java配置配置多个身份验证提供程序

Spring安全-如何使用java配置配置多个身份验证提供程序,java,authentication,spring-security,spring-java-config,Java,Authentication,Spring Security,Spring Java Config,我正在尝试配置一个具有多个身份验证机制(DB和LDAP)并使用spring安全性作为其底层框架的应用程序。我正在使用java配置来设置web和http安全性。我知道我们需要多个WebSecurityConfigureAdapter实例来支持多个http元素(在基于xml的配置中使用);但当我这样做时,应用程序只会选择配置的第一个身份验证(数据库身份验证),而不会使用第二个身份验证(ldap身份验证)。有什么原因吗?下面是代码片段 @EnableWebSecurity @EnableGlobalM

我正在尝试配置一个具有多个身份验证机制(DB和LDAP)并使用spring安全性作为其底层框架的应用程序。我正在使用java配置来设置web和http安全性。我知道我们需要多个WebSecurityConfigureAdapter实例来支持多个http元素(在基于xml的配置中使用);但当我这样做时,应用程序只会选择配置的第一个身份验证(数据库身份验证),而不会使用第二个身份验证(ldap身份验证)。有什么原因吗?下面是代码片段

@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration{

@Configuration
@Order(1)
public static class DBSecurityConfig extends WebSecurityConfigurerAdapter {

 @Inject
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .userDetailsService(userDetailsService);
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
            .antMatchers("/scripts/**/*.{js,html}")
            .antMatchers("/console*");
    }

 protected void configure(HttpSecurity http) throws Exception {

        http.csrf()
            .addFilterAfter(new CsrfCookieGeneratorFilter(), CsrfFilter.class)
            .exceptionHandling()
            .authenticationEntryPoint(authenticationEntryPoint)
            .and()
            .formLogin()
            .loginProcessingUrl("/api/authentication")
            .successHandler(ajaxAuthenticationSuccessHandler)
            .failureHandler(ajaxAuthenticationFailureHandler)
            .usernameParameter("j_username")
            .passwordParameter("j_password")
            .permitAll()
            .and()
            .logout()
            .logoutUrl("/api/logout")
            .logoutSuccessHandler(ajaxLogoutSuccessHandler)
            .deleteCookies("JSESSIONID")
            .permitAll()
            .and()
            .headers()
            .frameOptions()
            .disable()
            .and()
            .authorizeRequests()
            .antMatchers("/api/**").permitAll()
            .antMatchers("/api*//**").authenticated();
         }
     }

@Configuration
public static class LDAPSecurityConfig extends WebSecurityConfigurerAdapter {


    @Inject
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.ldapAuthentication()
            .userDnPatterns("uid={0},ou=people")
            .groupSearchBase("ou=groups")
            .contextSource()
            .ldif("classpath:users.ldif");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
            .antMatchers("/console*");
    }

    protected void configure(HttpSecurity http) throws Exception {
        http.csrf()
            .addFilterAfter(new CsrfCookieGeneratorFilter(), CsrfFilter.class)
            .exceptionHandling()
            .authenticationEntryPoint(ldapAuthenticationEntryPoint)
            .and()
            .formLogin()
            .loginProcessingUrl("/api/ldapAuthentication")
            .successHandler(ldapAjaxAuthenticationSuccessHandler)
            .failureHandler(ldapAjaxAuthenticationFailureHandler)
            .usernameParameter("j_username")
            .passwordParameter("j_password")
            .permitAll()
            .and()
            .logout()
            .logoutUrl("/api/logout")
            .logoutSuccessHandler(ajaxLogoutSuccessHandler)
            .deleteCookies("JSESSIONID")
            .permitAll()
            .and()
            .headers()
            .frameOptions()
            .disable()
            .and()
            .authorizeRequests()
            .antMatchers("/api/**").permitAll()
            .antMatchers("/api*//**").authenticated();
    }
}
为了简洁起见,我编辑了一些代码。任何关于它为什么不接受ldap身份验证的见解都值得赞赏


谢谢

以前的帖子,但我自己刚刚找到了答案。在
auth
对象中实现的生成器使用您提供的内容构建一个
AuthenticationManager
。您拥有的每个configurer实例都将尝试执行相同的操作,但您的应用程序实际上只使用其中一个AuthenticationManager对象。

以前的帖子,但我自己刚刚发现了答案。在
auth
对象中实现的生成器使用您提供的内容构建一个
AuthenticationManager
。您拥有的每个configurer实例都将尝试执行相同的操作,但您的应用程序实际上只使用一个生成的AuthenticationManager对象