spring-boot - 使用spring嵌入式ldap模拟事件目录进行集成测试

标签 spring-boot spring-security active-directory spring-security-ldap ldif

我正在使用 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/

相关文章:

spring - 从Spring依赖管理插件中提取依赖版本

java - 无法执行目标 org.codehaus.mojo :exec-maven-plugin:1. 2.1:exec (

java - 使用 spring-security-oauth2 的 SSO : Authentication Code never read

java - Spring boot + OAuth2 安全和请求过滤器

java - Spring Oauth2 中 oauth/token 的 CORS

oauth-2.0 - 在门户中哪里可以获取我的 Azure AD B2C 发行人 URL

java - Spring Boot 与 Thymeleaf - 空上下文

java - Spring Boot RestTemplate : Error 415 Unsupported Media Type

node.js - ldap nodejs 事件目录认证

Java:通过 Ldap 在 Active Directory 中创建用户时出现错误代码 53