2014-11-06 92 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); 
     } 
    } 
} 
相關問題