【发布时间】: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