使用Spring Boot应用程序中的AD LDP进行LDAP身份验证

时间:2017-11-30 11:24:46

标签: spring authentication spring-boot active-directory ldap

我正在尝试在Sprint Boot应用程序中实现LDAP身份验证。在测试环境中,我安装了一个用于进行身份验证的Active Directory LDP服务。我在AD实例中创建了一个用户,启用了帐户并设置了密码。然后我尝试使用Spring登录表单中的此帐户进行身份验证。

当我尝试使用AD登录时,收到错误消息:

  

您的登录尝试失败,请重试。

     

原因:凭据错误

由于我是AD和Spring的新手,我很可能配置错误(或两者都有!)。

您对我如何进一步诊断此问题有任何建议吗?或者我可能错过了哪些明显的事情?

我的Spring Boot代码(我在这段代码上尝试了很多不同的变体,这是一个例子):

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(activeDirectoryLdapAuthenticationProvider());
    }

    @Bean
    public AuthenticationManager authenticationManager() {
        return new ProviderManager(Arrays.asList(activeDirectoryLdapAuthenticationProvider()));
    }

    @Bean
    public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
        ActiveDirectoryLdapAuthenticationProvider provider = 
                new ActiveDirectoryLdapAuthenticationProvider("foo.bar", "ldap://servername:389");
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationRequestCredentials(true);
        return provider;
    }
}

1 个答案:

答案 0 :(得分:1)

事实证明我的Java实现没有任何问题。问题似乎与AD LDP配置有关。我尝试连接到另一个已知良好的AD LDP实例并且第一次进行身份验证。

我打算将此标记为答案,因为我不再对这个问题的解决方案感兴趣并希望将其关闭......