与 LDAP 通信时总是收到 Bad Credential

Always getting Bad Credential while communicating with LDAP

我在与 LDAP 通信时收到错误的凭据,

我设置了管理员用户(并且可以正常登录 Windows Server 2012)并在服务器上安装了 Active Directory 域服务。 我不认为我的应用程序有任何问题,因为它在嵌入 .ldif 文件的情况下工作正常 spring,我也尝试过使用下面的开放服务器,它也工作正常。

LDAP_Server_Details

我也在分享 DC 结构,如果我从代码中传递了任何错误的信息,请纠正我。

如有任何帮助,我们将不胜感激

配置

@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http)
        throws Exception {
    System.out.println(
            "WebSecurityConfig configure(HttpSecurity http)");
    http.authorizeRequests().anyRequest()
            .fullyAuthenticated().and().formLogin();
}

@Override
public void configure(AuthenticationManagerBuilder auth)
        throws Exception {
    System.out.println(
            "WebSecurityConfig configure(AuthenticationManagerBuilder auth)");
    auth.ldapAuthentication()
            .userDnPatterns("uid={0},ou=Users")
            //.groupSearchBase("ou=groups")
            .contextSource(contextSource()).passwordCompare()
            .passwordEncoder(new LdapShaPasswordEncoder())
            .passwordAttribute("userPassword");
}

@Bean
public DefaultSpringSecurityContextSource contextSource() {
    System.out.println(
            "DefaultSpringSecurityContextSource contextSource()");
    return new DefaultSpringSecurityContextSource(
            Arrays.asList("ldap://test.local:389/"),
            "dc=test,dc=local");
}

}

即使 Active Directory 是一种 LDAP,标准的 LDAP 支持(在 Spring 安全性中)也不适用于它。相反,您应该使用专门的 ActiveDirectoryLdapAuthenticationProvider:

@Bean
public ActiveDirectoryLdapAuthenticationProvider adProvider() {
    return new ActiveDirectoryLdapAuthenticationProvider("YOURDOMAIN", "ldap://test.local:389/");
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(adProvider());
}

不要忘记将 YOURDOMAIN 更改为您要登录的域。