如何接受 JNDI/LDAP 连接的自签名证书?
我需要通过 SSL 连接到 LDAP 目录。
在非生产环境中,我们使用自签名证书,当然,该证书无法验证:
javax.naming.CommunicationException: simple bind failed: ldapserver: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]
at com.sun.jndi.ldap.LdapClient.authenticate(LdapClient.java:197)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2694)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:293)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:134)
我知道如何使用 用于启用 SSL 的连接的自定义信任管理器,但不知道如何将其与 JNDI API 结合使用我不管理实际连接。也就是说,我可以在哪里插入信任管理器的以下标准设置?
提前致谢。
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldaps://ldapserver:636");
env.put(Context.SECURITY_PROTOCOL, "ssl");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "myUser");
env.put(Context.SECURITY_CREDENTIALS, "myPassword");
LdapContext ctx = new InitialLdapContext(env, null);
ctx.search (...)
I need to connect to an LDAP directory over SSL.
In non-production environments, we use self-signed certificates which, of course, fails to validate with:
javax.naming.CommunicationException: simple bind failed: ldapserver: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]
at com.sun.jndi.ldap.LdapClient.authenticate(LdapClient.java:197)
at com.sun.jndi.ldap.LdapCtx.connect(LdapCtx.java:2694)
at com.sun.jndi.ldap.LdapCtx.<init>(LdapCtx.java:293)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:175)
at com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:193)
at com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:136)
at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:66)
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:667)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
at javax.naming.InitialContext.init(InitialContext.java:223)
at javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:134)
I am aware of how to use a custom trust manager for SSL-enabled connections, but don't know how to use one in connection with the JNDI API where I don't manage the actual connection. That is, where is the following standard setup will I be able to plug the trust manager?
Thanks in advance.
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldaps://ldapserver:636");
env.put(Context.SECURITY_PROTOCOL, "ssl");
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, "myUser");
env.put(Context.SECURITY_CREDENTIALS, "myPassword");
LdapContext ctx = new InitialLdapContext(env, null);
ctx.search (...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
根据 JNDI 文档,似乎可以设置自定义
SSLSocketFactory
http://download.oracle.com/javase/1.5.0/docs/guide/jndi/jndi-ldap-gl.html#socket
配置使用此套接字工厂的环境
According to the JNDI documentation it seems possible to set a custom
SSLSocketFactory
http://download.oracle.com/javase/1.5.0/docs/guide/jndi/jndi-ldap-gl.html#socket
Configure the environment to use this socket factory
当您覆盖 Trustmanager 时,您可以接受任何证书:
DummyTrustmanager.java
MySSLSocketFactory.java
Main.java
You could accept any certificate when you override the Trustmanager:
DummyTrustmanager.java
MySSLSocketFactory.java
Main.java
无需代码。只需将证书添加到测试客户端信任库即可。
不要为此编写额外的代码。它是不安全的,并且您面临着代码泄漏到生产中的重大风险。你真的不想要这样。
No code required. Just add the certificate to the test client truststores.
Don't write extra code for this. It is insecure, and you run a major risk of the code leaking into production. You really don't want that.
@Jcs 答案是正确的,但如果您不想使用自定义 TrustManager,并且您愿意将该特定自签名证书作为虚拟机内其他用途的受信任 CA,您可以:
cacerts
),或者cacerts
的副本),这将包含该特定证书,并将其用作默认信任库,但设置 javax.net.ssl.trustStore* 系统属性。@Jcs answer is correct, but if you don't want to use a custom TrustManager, and if you're willing for that particular self-signed certificate to be a trusted CA for other uses within the VM, you can either:
cacerts
under the JRE security directory) orcacerts
) which would contain that particular certificate, and use this as your default truststore, but setting thejavax.net.ssl.trustStore*
system properties.自从我上次这样做以来已经过去了大约 5 年,所以恐怕我的答案会有点模糊;但我认为如果你在 java.security 中实现了一个可信的提供者,你应该能够在 /usr/java/jre/lib/security/ 下找到它;然后它将接受该证书作为可信证书。
我目前无法访问我的笔记,但稍后我会仔细研究它们
Its been about 5 years since I last did this so my answer will be a bit vague I'm afraid; but I think if you impliment a trusted provider in the java.security which you should be able to find under /usr/java/jre/lib/security/; it will then accept the certificate as trusted.
I dont have access to my notes at the moment, but I'll have a dig through them later
完全抛开 JNDI,一些框架将引用 LOCAL SYSTEM LDAP 配置(通常是 /etc/ldap.conf 或类似的东西)。当我说“本地”时,我指的是运行 JNDI 操作的系统。
至少在大多数支持 LDAP 的 Linux 操作系统中,此类 LDAP 配置文件的内部(通常)有一行内容:
这是默认的 TLS/SSL 关键性设置(也是最严格的),因为它会使如果证书有任何问题(包括自签名),则连接。
您可以尝试不同的设置而不是“需求”(使用“允许”或“从不”)。完成此操作后,再次尝试您的操作,看看您的问题是否消失。同样,并非所有类似的东西都会读取甚至检测您的本地系统设置。有些会,有些不会。值得一看。
我希望这会有所帮助...
麦克斯
Setting aside JNDI entirely, some frameworks will refer to the LOCAL SYSTEM LDAP configuration (this is typically /etc/ldap.conf or something like that). When I say LOCAL, I mean the system(s) on which your JNDI operation is running.
At least in the case of most LDAP-enabled Linux OSs, inside of such an LDAP config file is (usually) a line that reads:
This is the default TLS/SSL criticality setting (and the most strict), as it will fail the connection if ANYTHING WHATSOEVER is wrong with the certificate (this includes being self-signed).
You can experiment with different settings instead of 'demand' (play with 'allow' or 'never'). Once this is done, try your operation again and see if your issue subsides. Again, not all things like this will read or even detect your local system settings. Some do, some don't. Worth a look.
I hope this helps...
Max