Spring Boot 应用程序中的 LDAP 身份验证

Posted

技术标签:

【中文标题】Spring Boot 应用程序中的 LDAP 身份验证【英文标题】:LDAP authentication in spring boot app 【发布时间】:2018-04-15 02:05:24 【问题描述】:

我对 LDAP 几乎一无所知,对 spring 安全性更是一无所知,但我正在尝试配置一个 spring boot 应用程序来针对 ldap 实例进行身份验证,但我被卡住了。

我在 adldap.company.com 获得了 ldap 服务器名称和 dc=ad,dc=company,dc=com 的基本 dn

我有一些 python 代码可以进行简单的绑定并且可以工作。

LDAP_USERNAME = 'username@ad.company.com'
LDAP_PASSWORD = 'password'
base_dn = 'dc=ad,dc=company,dc=com' # not used for bind I guess, only search
try:
    ldap_client = ldap.initialize('ldap://adldap.company.com')
    ldap_client.set_option(ldap.OPT_REFERRALS,0)
    ldap_client.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)
except ldap.INVALID_CREDENTIALS as e:
    ldap_client.unbind()
    return 'Wrong username and password: %s' % e
except ldap.SERVER_DOWN:
   return 'AD server not available'

如果我运行此代码,它似乎成功绑定为“username@ad.company.com”,密码为“password”。

我还有一个我认为应该处理身份验证的 WebSecurityConfig 类:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter 

    @Override
    protected void configure(HttpSecurity http) throws Exception 
        http
            .antMatcher("/secure")
            .authorizeRequests()
            .anyRequest().fullyAuthenticated()
            .and()
            .httpBasic();
    

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception 
        auth
            .ldapAuthentication()
            .userDnPatterns("uid=0")
            .contextSource()
            .url("ldap://adldap.company.com");
            //.url("ldap://adldap.company.com/dc=ad,dc=company,dc=com");
    

当我在应用程序中转到 /secure 时,我会弹出一个基本的身份验证,但是我尝试输入的任何内容都会让我 401 Unauthorized。我试过“username@ad.company.com”,没有域,把这些东西放在 userDnPatterns 中,比如 0@adldap.company.com 和一堆其他东西。我尝试过使用不同的 URL,其中包含基本 dn。似乎没有任何效果。我错过了什么?

另外,这是对用户进行身份验证的正确方法吗?我已经阅读了有关绑定身份验证和绑定和搜索的内容,但服务器不允许匿名绑定,所以我想我需要某种可以绑定和执行搜索的“应用程序用户”,对吧?那是“更好”吗?

【问题讨论】:

您是否在 org.springframework.security.ldap 上启用了 DEBUG 日志记录? 是的,它基本上给了我一个 InvalidCredentials 错误。不过密码没问题,就是觉得格式有问题。 @SébastienHelbert 我知道已经有一段时间了,但我在 atm 面临类似的问题。你能看看我的question,看看你是否也遇到过同样的麻烦,能帮我解决吗? :) 【参考方案1】:

Active Directory 有自己的非标准用户身份验证语法,不同于通常的 LDAP DN 绑定。

Spring Security 为 Active Directory 提供了一个专门的 AuthenticationProvider。

试试这个:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter 

    @Override
    protected void configure(HttpSecurity http) throws Exception 
        http
            .antMatcher("/secure")
            .authorizeRequests()
            .anyRequest().fullyAuthenticated()
            .and()
            .httpBasic();
    

    @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("adldap.company.com", "ldap://adldap.company.com");
        provider.setConvertSubErrorCodesToExceptions(true);
        provider.setUseAuthenticationRequestCredentials(true);
        return provider;
    

【讨论】:

这真的很有用。但是我想知道如果我不使用.httpBasic() Spring Security 提供的默认身份验证类型是什么?【参考方案2】:

长话短说,问题在于 Microsoft Active Directory LDAP 不是“普通”LDAP,因此您需要以不同的方式连接到它。

工作解决方案在这里:https://medium.com/@dmarko484/spring-boot-active-directory-authentication-5ea04969f220

【讨论】:

以上是关于Spring Boot 应用程序中的 LDAP 身份验证的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Spring Boot 中为 Spring LDAP 身份验证设置覆盖 BindAuthenticator handleBindException

spring boot ldap 组和受限端点

Spring boot Ldap 身份验证失败,LDAP 错误代码 49 - 80090308 数据 52e

如何使用 Spring Boot 连接外部/在线 LDAP 服务器?

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

如何让 Spring Boot Actuator LdapHealthIndicator 与 Spring Security 的 Ldap 一起运行?