2014-11-06 178 views
9

我目前正在使用Spring Boot创建一个新的Web应用程序,并开始整合Spring Security进行身份验证的过程。在成功地遵循基于Spring Boot的LDAP tutorial之后,我想将基于JavaConfig的配置指向我的Active Directory实例。PartialResultException当使用Spring Security和JavaConfig进行身份验证时

我的应用程序现在可以处理不好凭证预期,但有效凭据现在导致

javax.naming.PartialResultException: Unprocessed Continuation Reference(s); remaining name '' 

这是一个常见的问题 - 有些情况下已经遇到此问题的numberofplaces。该解决方案似乎将Context.REFERRAL设置为“跟随”,但我找不到任何文档指出如何使用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>"); 
     } 
    } 
} 

回答

13

我有我需要使用的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); 
     } 
    } 
} 
相关问题