使用 ActiveDirectoryLdapAuthenticationProvider 和嵌入式 ldif 进行 Spring 身份验证

Mac*_*ski 6 java authentication spring ldap active-directory

我的客户要求之一是根据他的公司 ActiveDirectory (LDAP) 对用户进行身份验证。所以我使用了标准的 ActiveDirectoryLdapAuthenticationProvider,它的工作方式就像一个魅力。

@Bean
public ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
    ActiveDirectoryLdapAuthenticationProvider adProvider = new ActiveDirectoryLdapAuthenticationProvider(
            ldapConfig.getLdapDomain(), ldapConfig.getLdapUrl(), ldapConfig.getLdapRoot());
    adProvider.setConvertSubErrorCodesToExceptions(true);
    adProvider.setUseAuthenticationRequestCredentials(true);
    adProvider.setSearchFilter(ldapConfig.getLdapSearchFilter());
    return adProvider;
}
Run Code Online (Sandbox Code Playgroud)

问题是客户端 AC 隐藏在防火墙后面。它在部署后可以工作,但由于客户端安全策略,我在本地开发期间无法访问 AC。因此,我有一个想法,也许对于开发配置文件,我将使用嵌入式 LDAP 服务器(UnboundID LDAP SDK for Java)。我不是 LDAP 专家,但我以某种方式编写了简单的 ldif 文件,它看起来像这样:

dn: dc=test,dc=local
objectclass: top
objectclass: domain
objectclass: extensibleObject
dc: test
# Organizational Units
dn: ou=groups,dc=test,dc=local
objectclass: top
objectclass: organizationalUnit
ou: groups
dn: ou=people,dc=test,dc=local
objectclass: top
objectclass: organizationalUnit
ou: people
# Users
dn: uid=john,ou=people,dc=test,dc=local
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: John Doe
sn: John
uid: john
password: johnspassword
# Create Groups
dn: cn=administrators,ou=groups,dc=test,dc=local
objectclass: top
objectclass: groupOfUniqueNames
cn: administrators
ou: administrator
uniqueMember: uid=john,ou=people,dc=test,dc=local
Run Code Online (Sandbox Code Playgroud)

我还配置了嵌入式 ldap 属性:

spring.ldap.embedded.base-dn=dc=test,dc=local
spring.ldap.embedded.ldif=classpath:localldapactivedirectory.ldif
spring.ldap.embedded.port=12345
spring.ldap.embedded.url=ldap://localhost:12345/
spring.ldap.embedded.validation.enabled=false
Run Code Online (Sandbox Code Playgroud)

UnboundID 对此没有问题,但正如您猜测的那样,我在身份验证过程中遇到了问题:

[LDAP: error code 34 - Unable to parse bind DN 'john@test.local':  Unable to parse string 'john@test.local' as a DN because it does not have an equal sign after RDN attribute 'john@test.local'.]; nested exception is javax.naming.InvalidNameException: [LDAP: error code 34 - Unable to parse bind DN 'john@test.local':  Unable to parse string 'user_cms@test.com' as a DN because it does not have an equal sign after RDN attribute 'john@test.local'.]
Run Code Online (Sandbox Code Playgroud)

为了向您展示整个情况,我添加了身份验证尝试期间发生的错误(在自定义身份验证服务中):

Authentication authentication = authenticationManagerBuilder.getObject()
            .authenticate(authenticationToken);
Run Code Online (Sandbox Code Playgroud)

我认为这是由于我的 ldif 文件太简单造成的。它可以与普通的 Ldap 身份验证提供程序一起使用,但不能与 ActiveDirectoryLdapAuthenticationProvider 一起使用。

有谁知道如何在 ldif 文件中“模拟”ActiveDirectory(LDAP),以便 ActiveDirectoryLdapAuthenticationProvider 可以在身份验证期间使用它?

RoB*_*ToZ 1

我遇到了同样的问题并找到了有效的解决方案,但我对此不太满意。它对我来说是这样的:

  1. 调整.ldif文件

    将属性添加userPrincipalName到您的.ldif文件中,并在您的情况下使用与 相同的值填充它dn

    dn: uid=john,ou=people,dc=test,dc=local
    userPrincipalName: uid=john,ou=people,dc=test,dc=local
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用dn代替电子邮件地址进行身份验证

    john@test.local您在测试中使用身份验证。要使其工作,您必须使用dnuid=john,ou=people,dc=test,dc=local

经过这些更改后,针对嵌入式 LDAP 的身份验证在测试中发挥了作用。欢迎更好的解决方案!

  • 不幸的是它没有帮助,有下一个错误:引起:javax.naming.AuthenticationException:[LDAP:错误代码49 - 无法绑定为用户'uid = super,ou = people,dc = test,dc = com@test .com',因为服务器中不存在这样的条目。] (2认同)