gpt4 book ai didi

java - 如何接受 JNDI/LDAP 连接的自签名证书?

转载 作者:搜寻专家 更新时间:2023-10-30 21:14:50 25 4
gpt4 key购买 nike

我需要通过 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)

我知道如何使用 custom trust manager for SSL-enabled connections ,但不知道如何将其与我不管理实际连接的 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 (...)

最佳答案

根据 JNDI 文档,似乎可以设置自定义 SSLSocketFactory

http://download.oracle.com/javase/1.5.0/docs/guide/jndi/jndi-ldap-gl.html#socket

public class MySSLSocketFactory extends SocketFactory {
private static final AtomicReference<MySSLSocketFactory> defaultFactory = new AtomicReference<>();

private SSLSocketFactory sf;

public MySSLSocketFactory() {
KeyStore keyStore = ... /* Get a keystore containing the self-signed certificate) */
TrustManagerFactory tmf = TrustManagerFactory.getInstance();
tmf.init(keyStore);
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(null, tmf.getTrustManagers(), null);
sf = ctx.getSocketFactory();
}

public static SocketFactory getDefault() {
final MySSLSocketFactory value = defaultFactory.get();
if (value == null) {
defaultFactory.compareAndSet(null, new MySSLSocketFactory());
return defaultFactory.get();
}
return value;
}

@Override
public Socket createSocket(final String s, final int i) throws IOException {
return sf.createSocket(s, i);
}

@Override
public Socket createSocket(final String s, final int i, final InetAddress inetAddress, final int i1) throws IOException {
return sf.createSocket(s, i, inetAddress, i1);
}

@Override
public Socket createSocket(final InetAddress inetAddress, final int i) throws IOException {
return sf.createSocket(inetAddress, i);
}

@Override
public Socket createSocket(final InetAddress inetAddress, final int i, final InetAddress inetAddress1, final int i1) throws IOException {
return sf.createSocket(inetAddress, i, inetAddress1, i1);
}
}

配置环境使用这个套接字工厂

env.put("java.naming.ldap.factory.socket", "com.example.MySSLSocketFactory");

关于java - 如何接受 JNDI/LDAP 连接的自签名证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4615163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com