如何接受JNDILDAP连接的自签名证书?

如何接受JNDILDAP连接的自签名证书?,第1张

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

根据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 = ...         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");


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5020781.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存