我正在使用 Spring Security ActiveDirectoryLdapAuthenticationProvider
使用 Spring Boot(基于注释的配置)向 Active Directory 进行身份验证并生成 token 。一切正常。
我希望添加一些模拟整个过程的集成测试,并且我正在考虑使用 Spring 嵌入式 LDAP 服务器。
我添加了从网上找到的另一个示例中获得的这个 ldif 文件。
#Actual test data
dn: dc=test,dc=com
objectclass: top
objectclass: domain
objectclass: extensibleObject
dc: local
# Organizational Units
dn: ou=groups,dc=test,dc=com
objectclass: top
objectclass: organizationalUnit
ou: groups
dn: ou=people,dc=test,dc=com
objectclass: top
objectclass: organizationalUnit
ou: people
# Create People
dn: uid=testuser,ou=people,dc=test,dc=com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Test
sn: User
uid: testuser
password: secret
# Create Groups
dn: cn=developers,ou=groups,dc=test,dc=com
objectclass: top
objectclass: groupOfUniqueNames
cn: developers
ou: developer
uniqueMember: uid=testuser,ou=people,dc=test,dc=com
dn: cn=managers,ou=groups,dc=test,dc=com
objectclass: top
objectclass: groupOfUniqueNames
cn: managers
ou: manager
uniqueMember: uid=testuser,ou=people,dc=test,dc=com
但这当然不包括任何 Active Directory 架构的东西。
每个用户需要有一个
sAMAccountName
并且需要有memberOf
属性来确定它在哪个组中。有什么方法可以使它的行为类似于事件目录,以便 Spring
ActiveDirectoryLdapAuthenticationProvider
使用用户的用户名和密码绑定(bind)到它并获取其组成员身份以填充其权限?否则,如果这不可行,还有其他方法可以模拟它并进行适当的测试吗?
最佳答案
您可以使用提供 Apache DS 的 spring ldap-testing 依赖项来设置嵌入式 ldap 服务器。看
文章:https://www.baeldung.com/spring-ldap#testing
来源:https://github.com/eugenp/tutorials/blob/master/spring-ldap/src/test/java/com/baeldung/ldap/javaconfig/TestConfig.java
您可以使用的其他内存 LDAP Java 实现包括:
https://docs.ldap.com/ldap-sdk/docs/in-memory-directory-server.html
// Create the configuration to use for the server.
InMemoryDirectoryServerConfig config =
new InMemoryDirectoryServerConfig("dc=example,dc=com");
config.addAdditionalBindCredentials("cn=Directory Manager", "password");
// Create the directory server instance, populate it with data from the
// "test-data.ldif" file, and start listening for client connections.
InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
ds.importFromLDIF(true, "test-data.ldif");
ds.startListening();
或:https://github.com/inbloom/ldap-in-memory如果您更喜欢类似生产的场景,您还可以在测试容器中使用完整的 ldap 服务器。
关于spring-boot - 使用spring嵌入式ldap模拟事件目录进行集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46641857/