我目前正在使用Spring Boot创建一个新的Web应用程序,并开始了集成Spring Security进行身份验证的过程。成功遵循基于Spring Boot的LDAP tutorial之后,我想将基于JavaConfig的配置指向我的Active Directory实例。
我的应用程序现在可以按预期处理错误的凭据,但是有效的凭据现在导致
javax.naming.PartialResultException: Unprocessed Continuation Reference(s); remaining name ''
这是一个常见问题-遇到此问题的地方是number of places。解决方案似乎是将Context.REFERRAL设置为“follow”,但是我找不到任何文档说明如何使用JavaConfig设置该选项。在这里,我唯一的选择是恢复为基于XML的配置吗?似乎Spring正在将开发人员推向JavaConfig,因此,如果可能的话,我想避免混合使用这两种方法。
以下是我的安全配置:
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/css/**").permitAll().anyRequest()
.fullyAuthenticated().and().formLogin();
}
@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication()
.userSearchBase("")
.userSearchFilter("(&(cn={0}))").contextSource()
.managerDn("<username>")
.managerPassword("<password>")
.url("ldap://<url>");
}
}
}
最佳答案
我觉得我需要使用LdapContextSource
实例来实现此目的(因为它方便地具有setReferral
方法),但是我在细节方面有些挣扎。 spring.io上的forum post给了我足够的力量,看起来我现在可以正常工作了。
我目前尚不清楚我在这里所做的工作是否存在任何重大缺陷,但似乎可以奏效,因此希望这对以后的其他人会有所帮助:
@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/css/**").permitAll().anyRequest()
.fullyAuthenticated().and().formLogin();
}
@Configuration
protected static class AuthenticationConfiguration extends
GlobalAuthenticationConfigurerAdapter {
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource("ldap://<url>");
contextSource.setUserDn("<username>");
contextSource.setPassword("<password>");
contextSource.setReferral("follow");
contextSource.afterPropertiesSet();
LdapAuthenticationProviderConfigurer<AuthenticationManagerBuilder> ldapAuthenticationProviderConfigurer = auth.ldapAuthentication();
ldapAuthenticationProviderConfigurer
.userSearchFilter("(&(cn={0}))")
.userSearchBase("")
.contextSource(contextSource);
}
}
}