2017-05-11 71 views
0

我想通过使用Spring Security的Spring LDAP(2.3.1.RELEASE)将自己登录到现有的AD中。到目前为止,我已经得到了下面的代码负责配置(从Spring getting started guide almostly拍摄):通过Spring LDAP和Spring安全连接到现有的广告

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
     .authorizeRequests() 
     .antMatchers("/", "/home").permitAll() 
     .anyRequest().fullyAuthenticated() 
     .and() 
     .formLogin() 
     .loginPage("/login") 
     .permitAll() 
     .and() 
     .logout() 
     .permitAll(); 
} 

@Override 
public void configure(AuthenticationManagerBuilder auth) throws Exception { 
    authauth 
     .ldapAuthentication() 
     .userDnPatterns("uid={0}") 
     .contextSource(contextSource()) 
     .passwordCompare() 
     .passwordEncoder(new LdapShaPasswordEncoder()) 
     .passwordAttribute("userPassword"); 
} 


@Bean 
public BaseLdapPathContextSource contextSource() { 
    LdapContextSource contextSource = new LdapContextSource(); 
    contextSource.setAnonymousReadOnly(false); 
    contextSource.setBase("dc=foo,dc=bar"); 
    contextSource.setUserDn("[email protected]"); 
    contextSource.setPassword("plainTextPassword"); 
    contextSource.setUrl("ldap://active.directory.address"); 
    return ldapContextSource; 
} 
} 

这里是我的POM片段:

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.2.RELEASE</version> 
</parent> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-thymeleaf</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.ldap</groupId> 
     <artifactId>spring-ldap-core</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-ldap</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>com.unboundid</groupId> 
     <artifactId>unboundid-ldapsdk</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.security</groupId> 
     <artifactId>spring-security-test</artifactId> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.directory.server</groupId> 
     <artifactId>apacheds-all</artifactId> 
     <version>1.5.5</version> 
    </dependency> 
</dependencies> 

<properties> 
    <java.version>1.8</java.version> 
</properties> 

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</build> 

我故意省略了.html文件,因为我认为他们在这个问题上不起任何作用。 Atm我得到

javax.naming.AuthenticationException: [LDAP: error code 49 - 80090308: LdapErr: DSID-0C0903D9, comment: AcceptSecurityContext error, data 52e, v2580 ] 

结果。在搜索异常之后,我发现数据52e意味着AD_INVALID CREDENTIALS。我多次查看我的凭证,他们确实是对的。我甚至下载了Microsofts AD explorer,并使用这些凭据成功连接到AD。为什么不通过代码工作,并通过AD浏览器工作?

回答

0

这个错误是什么时候发生的?在应用程序启动时还是尝试登录时? 如果我在上下文源中输入无效凭据,则会出现此错误。如果你确定你的密码是正确的,你应该检查你的base和userDN。

您可以发布您的LDAP树或用户在上下文源中使用的列出的LDIF部分吗?