You need to enable JavaScript to run this app. 如何测试Spring Boot中的LDAP安全配置并模拟LDAP响应?
优惠活动
大模型
产品
解决方案
定价
更多

如何测试Spring Boot中的LDAP安全配置并模拟LDAP响应?

核心Mock对象说明

你要实现LDAP返回结果模拟,不需要从底层LDAP链接开始Mock,根据你的测试目的不同,选对应组件即可:

  • 若仅测试认证流程是否符合预期:直接Mock LdapUserSearch 接口即可,这是Spring Security LDAP认证时用来搜索用户的核心接口,替换后你可以完全自定义搜索返回的用户属性。
  • 若要验证你写的userSearchFilter规则是否正确:推荐用嵌入式LDAP服务器做集成测试,不需要写Mock逻辑,更贴近真实运行效果。

方案1:Mock LdapUserSearch实现轻量测试

你可以用@MockBean直接替换Spring容器中自动生成的LdapUserSearch实例,自定义返回符合要求的用户对象:

@SpringBootTest
@AutoConfigureMockMvc
public class LdapAuthTest {
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private LdapUserSearch ldapUserSearch;

    // 测试合法用户(属于TEAM-1组)认证通过
    @Test
    public void testValidUserAuthSuccess() throws Exception {
        // 构造符合要求的模拟LDAP用户
        DirContextOperations validUser = mock(DirContextOperations.class);
        when(validUser.getStringAttribute("initials")).thenReturn("A");
        when(validUser.getStringAttributes("memberOf")).thenReturn(new String[]{
            "CN=TEAM-1,OU=Intern,DC=my-company"
        });
        // 模拟搜索首字母为A的用户返回上述合法用户
        when(ldapUserSearch.searchForUser("A")).thenReturn(validUser);

        // 调用你的登录接口验证认证结果,参数根据你的业务调整
        mockMvc.perform(post("/login")
                .param("username", "A")
                .param("password", "test"))
                .andExpect(status().isOk());
    }

    // 测试非法用户(属于INVALID组)认证失败
    @Test
    public void testInvalidUserAuthFail() throws Exception {
        DirContextOperations invalidUser = mock(DirContextOperations.class);
        when(invalidUser.getStringAttribute("initials")).thenReturn("B");
        when(invalidUser.getStringAttributes("memberOf")).thenReturn(new String[]{
            "CN=Team-INVALID,OU=Intern,DC=my-company"
        });
        when(ldapUserSearch.searchForUser("B")).thenReturn(invalidUser);

        mockMvc.perform(post("/login")
                .param("username", "B")
                .param("password", "test"))
                .andExpect(status().isUnauthorized());
    }
}

注:如果你的业务需要校验LDAP密码,可以额外MockPasswordComparisonAuthenticator的密码校验逻辑,或者在测试配置中直接设置通用测试密码即可。


方案2:嵌入式LDAP测试过滤规则(推荐用于验证Filter正确性)

如果要真实校验你写的userSearchFilter是否生效,直接用Spring Boot自带的嵌入式LDAP服务器更简单:

  1. 引入测试依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-ldap</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.unboundid</groupId>
    <artifactId>unboundid-ldapsdk</artifactId>
    <scope>test</scope>
</dependency>
  1. 在application-test.yml中配置嵌入式LDAP:
spring:
  ldap:
    embedded:
      base-dn: DC=my-company
      ldif: classpath:test-users.ldif
      port: 33389
  1. 在test/resources下新建test-users.ldif,预先写入测试用户:
dn: OU=Intern,DC=my-company
objectClass: organizationalUnit
ou: Intern

# 合法用户
dn: CN=test1,OU=Intern,DC=my-company
objectClass: person
cn: test1
sn: test1
initials: A
memberOf: CN=TEAM-1,OU=Intern,DC=my-company
userPassword: 123456

# 非法用户
dn: CN=test2,OU=Intern,DC=my-company
objectClass: person
cn: test2
sn: test2
initials: B
memberOf: CN=Team-INVALID,OU=Intern,DC=my-company
userPassword: 123456
  1. 直接写测试用例调用认证管理器即可,不需要额外Mock,过滤规则会真实执行。

内容的提问来源于stack exchange,提问作者membersound

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.18 15:36:00