更新:我从未通过 SSL 实现此功能。为了获得安全性,我最终实现了 VPN。
我已经解决这个问题两天了,但始终无法解决这个问题。我查看了以下主题:
https://stackoverflow.com/questions/14465089/ssl-connection-in-glassfish-3-1
还有许多其他的。
更新:抱歉,我什至没有发布我收到的错误。这是:
javax.naming.CommunicationException: simple bind failed: server.local:636 [Root exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target]
我还在 Win7 pro 上使用 GlassFish server 3.1.2 和 NetBeans 7.3。
这是导致错误的代码:
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://server.local:636/");
// Specify SSL
env.put(Context.SECURITY_PROTOCOL, "ssl");
// Fill in secuirty/bind variables
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, config.Config.getSECURITY_PRINCIPAL()); //returns <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="097c7a6c7b496d66646860672765666a6865" rel="noreferrer noopener nofollow">[email protected]</a>
env.put(Context.SECURITY_CREDENTIALS, config.Config.getSECURITY_CREDENTIALS()); //returns password
// Create the initial context
ctx = new InitialDirContext(env); //defined above as InitialDirContext ctx = null;
我用过ldp.exe
确认 SSL 在我们的 AD 服务器上配置正确。此外,我还尝试了以下方法:
- 按照概述导入客户端证书(以及来自 AD CS 的 CA 根证书)here
a.我使用了以下命令:
C:\Program Files (x86)\Java\jdk1.7.0_25>bin\keytool -import -file SBS2011.sage.local_sage-SBS2011-CA.crt -keystore .\jre\lib\security\cacerts -alias SBS2011
Enter keystore password:
Certificate already exists in keystore under alias <mykey>
Do you still want to add it? [no]: yes
Certificate was added to keystore
C:\Program Files (x86)\Java\jdk1.7.0_25>
卸载 Java 并重新安装,然后重复步骤 1。
添加以下代码行:
System.setProperty("javax.net.ssl.trustStore", "C:\\Program Files (x86)\\Java\\jdk1.7.0_25\\jre\\lib\\security\\cacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "changeit");
其他注意事项:代码使用非 SSL 连接可以正常工作,但在尝试更新用户信息时出现 LDAP 错误 53。最后,如果有一个不使用 SSL 的解决方案,我不介意。
最佳答案
您的信任库不信任 LDAP 服务器证书。
上面的步骤 (3) 是默认的。
如果您的 LDAP 服务器具有 CA 签名的证书,则不需要步骤 (1)。
我不知道为什么您提到“客户端证书”,因为它是您可能需要导入的LDAP 服务器证书。
env.put(Context.PROVIDER_URL, "ldap://server.local:636/");
应该是
env.put(Context.PROVIDER_URL, "ldaps://server.local:636/");
关于java - 通过 Java 到 LDAP 的 SSL 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17348821/