2017-06-05 38 views
0

私はSpring SecurityによるLDAP認証を試みています。 Spring Security Active Directory

私のコード

...

auth.ldapAuthentication() 
      .userSearchFilter("(uid={0})").userSearchBase("ou=TTU") 
      .groupSearchFilter("uniqueMember={0}").groupSearchBase("ou=TTU") 
      .contextSource(contextSource()) 
      .passwordCompare() 
       .passwordEncoder(new LdapShaPasswordEncoder()) 
       .passwordAttribute("userPassword"); 

しかし、常に返し401 "悪い資格証明書" は何の間違いのようなものになることができますか? おそらく、誰かがJava設定の例を持っています。

答えて

0

それはうまくいきます...多分誰も助けてくれるでしょう。

 auth.authenticationProvider(ldapAuthenticationProvider()); 
     auth.eraseCredentials(true); 



@Bean 
public DefaultSpringSecurityContextSource contextSource(){ 

    DefaultSpringSecurityContextSource contextSource = 
      new DefaultSpringSecurityContextSource(Arrays.asList("ldap://url:389/"),"dc=ttu,dc=ru"); 
    contextSource.setUserDn(userDn); 
    contextSource.setPassword(passwordForLDAP); 
    contextSource.setReferral("follow"); 
    return contextSource; 
    } 

@Bean 
public LdapAuthenticationProvider ldapAuthenticationProvider(){ 
    return new LdapAuthenticationProvider(ldapAuthenticator(),ldapAuthoritiesPopulator()); 
} 

@Bean 
public LdapAuthenticator ldapAuthenticator(){ 
    BindAuthenticator authenticator = new BindAuthenticator(contextSource()); 
    authenticator.setUserSearch(userSearch()); 
    return authenticator; 
} 

@Bean 
public DefaultLdapAuthoritiesPopulator ldapAuthoritiesPopulator(){ 
    DefaultLdapAuthoritiesPopulator ldapAuthoritiesPopulator = 
      new DefaultLdapAuthoritiesPopulator(contextSource(),"ou=TTU"); 
    ldapAuthoritiesPopulator.setSearchSubtree(true); 
    ldapAuthoritiesPopulator.setIgnorePartialResultException(true); 
    //ldapAuthoritiesPopulator.setGroupSearchFilter("member={0}"); 
    ldapAuthoritiesPopulator.setRolePrefix("ROLE_"); 
    ldapAuthoritiesPopulator.setConvertToUpperCase(true); 
    return ldapAuthoritiesPopulator; 
} 

@Bean 
public FilterBasedLdapUserSearch userSearch(){ 
    FilterBasedLdapUserSearch filterBasedLdapUserSearch = 
      new FilterBasedLdapUserSearch("","(sAMAccountName={0})",contextSource()); 
    filterBasedLdapUserSearch.setSearchSubtree(true); 
    return filterBasedLdapUserSearch; 
}