gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:20:34 25 4
gpt4 key购买 nike

我正在尝试配置一个具有多种身份验证机制(DB 和 LDAP)并使用 spring security 作为其底层框架的应用程序。我正在使用 java 配置来设置 Web 和 http 安全性。我知道我们需要多个 WebSecurityConfigurerAdapter 实例用于多个 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。您拥有的配置器的每个实例都将尝试执行相同的操作,但您的应用程序实际上只会使用生成的 AuthenticationManager 对象之一。

关于java - Spring security - 如何使用 java config 配置多个身份验证提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33106106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com