【问题标题】:Spring security - How to configure multiple authentication providers using java configSpring security - 如何使用 java config 配置多个身份验证提供程序
【发布时间】:2016-01-11 09:45:48
【问题描述】:

我正在尝试配置一个具有多种身份验证机制(DB 和 LDAP)并使用 Spring Security 作为其底层框架的应用程序。我正在使用 java 配置来设置 web 和 http 安全性。我知道我们需要多个 WebSecurityConfigurerAdapter 实例用于多个 http 元素(在基于 xml 的配置中使用);但是当我这样做时,应用程序只会获取配置的第一个身份验证(数据库身份验证),而不会使用第二个身份验证(ldap 身份验证)进行身份验证。有什么理由吗?这是代码sn -p

@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 身份验证的见解都值得赞赏。

谢谢

【问题讨论】:

    标签: java authentication spring-security spring-java-config


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-05-23
      • 2016-04-22
      • 2012-02-18
      • 2012-03-07
      • 2014-05-01
      • 2015-08-16
      • 2018-05-07
      • 1970-01-01
      • 2019-08-08
      相关资源
      最近更新 更多