You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot实现LDAPS对接Active Directory身份验证的方案咨询

Spring Boot LDAPS Active Directory 认证配置解决方案

问题根因

你当前代码中定义的Context.SECURITY_PROTOCOL SSL协议配置哈希表未注入到ActiveDirectoryLdapAuthenticationProvider实例中,配置未实际生效,同时LDAPS场景默认需要信任AD服务器的SSL证书,否则会触发TLS握手失败异常。

落地实现步骤

1. 修正WebSecurityConfig配置

将环境配置注入到认证Provider实例,修正后的代码如下:

@Bean
public ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
    Hashtable<String, Object> env = new Hashtable<>();
    env.put(Context.SECURITY_PROTOCOL, "ssl");
    ActiveDirectoryLdapAuthenticationProvider activeDirectoryLdapAuthenticationProvider = new
            ActiveDirectoryLdapAuthenticationProvider("redacted.for.security", "ldaps://redacted.for.security:636/");
    // 新增行:将自定义环境配置注入到Provider
    activeDirectoryLdapAuthenticationProvider.setContextEnvironment(env);
    return activeDirectoryLdapAuthenticationProvider;
}

2. 配置AD SSL证书信任

二选一即可:

  • 方案A:导入证书到JDK信任库(适合固定部署环境)
    • 从AD服务器导出SSL根证书,保存为.cer格式文件
    • 执行JDK keytool命令导入证书:keytool -import -alias ad-ldaps-cert -file 你的证书本地路径.cer -keystore $JAVA_HOME/jre/lib/security/cacerts,默认信任库密码为changeit
    • 重启Spring Boot应用后生效
  • 方案B:代码级自定义信任库(适合多环境灵活部署)
    将信任库路径、密码配置添加到env哈希表中,无需修改JDK全局配置:
    env.put("javax.net.ssl.trustStore", "你的信任库本地路径");
    env.put("javax.net.ssl.trustStorePassword", "信任库密码");
    

3. 测试环境临时调优(生产环境禁止使用)

如果测试场景下需要跳过证书域名校验,可在应用启动参数中添加:-Dcom.sun.jndi.ldap.object.disableEndpointIdentification=true

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.13 19:03:01