java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > SpringLDAP连接LDAPS证书报错

SpringLDAP连接LDAPS证书报错问题及解决

作者:初心绘流年

这篇文章主要介绍了SpringLDAP连接LDAPS证书报错问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

一、问题背景

Java操作LDAP一般通过Spring LDAP比较方便,一般我们都是使用的常规的非加密的389端口

常规的初始化如下:

LdapContextSource contextSource = new LdapContextSource();
contextSource.setUserDn(config.getUsername());
contextSource.setPassword(config.getPassword());
String url = "ldap://" + config.getServer() + ":" + config.getPort();

contextSource.setUrl(url);
contextSource.setBase(config.getBaseDn());
contextSource.setAnonymousReadOnly(false);
contextSource.setPooled(false);
contextSource.afterPropertiesSet();

this.ldapTemplate = new LdapTemplate(contextSource);
this.ldapTemplate.setIgnorePartialResultException(true);

但是最近遇到一个使用证书加密环境的LDAP,即LDAPS(LDAP+SSL),使用的是636端口,再使用上述的配置,则会报错,可能会报以下的未找到合法证书的错误:

simple bind failed: 172.16.10.2:636; nested exception is javax.naming.CommunicationException: simple bind failed: 172.16.10.2:636 [Root exception is javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target]

二、解决方案

一般我们在Java使用HTTPS客户端的时候为了避免证书报错,一般会将客户端证书导入到JDK中,但是有些环境的证书是自签名的证书,导入也不一定能解决问题。

因此多数也会通过X509TrustManager和SSLSocketFactory绕过证书校验,所以我们对于LDAPS也采用同样的思路来解决,网上有类似的解决方案,但是集成之后可能还是存在以下的报错:

org.springframework.ldap.CommunicationException: simple bind failed: 172.16.10.2:636; nested exception is javax.naming.CommunicationException: simple bind failed: 172.16.10.2:636 [Root exception is javax.net.ssl.SSLHandshakeException: No subject alternative names matching IP address 172.16.10.2 found]
    at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:108)
    at org.springframework.ldap.core.support.AbstractContextSource.createContext(AbstractContextSource.java:355)
    at org.springframework.ldap.core.support.AbstractContextSource.doGetContext(AbstractContextSource.java:139)
    at org.springframework.ldap.core.support.AbstractContextSource.getReadOnlyContext(AbstractContextSource.java:158)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:357)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:309)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:642)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:578)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:1617)

simple bind failed: XXXXX.com:636; nested exception is javax.naming.CommunicationException: simple bind failed: XXXXX.com:636 [Root exception is javax.net.ssl.SSLHandshakeException: No subject alternative DNS name matching XXXXX.com found.]

org.springframework.ldap.CommunicationException: simple bind failed: 172.16.10.2:636; nested exception is javax.naming.CommunicationException: simple bind failed: 172.16.10.2:636 [Root exception is java.net.SocketException: Connection or outbound has closed]
    at org.springframework.ldap.support.LdapUtils.convertLdapException(LdapUtils.java:108)
    at org.springframework.ldap.core.support.AbstractContextSource.createContext(AbstractContextSource.java:355)
    at org.springframework.ldap.core.support.AbstractContextSource.doGetContext(AbstractContextSource.java:139)
    at org.springframework.ldap.core.support.AbstractContextSource.getReadOnlyContext(AbstractContextSource.java:158)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:357)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:309)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:642)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:578)
    at org.springframework.ldap.core.LdapTemplate.search(LdapTemplate.java:1617)

我的解决方案分为以下几个步骤,能规避以上错误:

(1)自定义SSLSocketFactory

package com.bugdongdong.utils.tools.ldap;

import javax.net.SocketFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class CustomSSLSocketFactory extends SSLSocketFactory {
    private SSLSocketFactory socketFactory;

    public CustomSSLSocketFactory() {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(null, new TrustManager[]{new DummyTrustmanager()}, new SecureRandom());
            socketFactory = ctx.getSocketFactory();
        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        }
    }

    public static SocketFactory getDefault() {
        return new CustomSSLSocketFactory();
    }

    @Override
    public String[] getDefaultCipherSuites() {
        return socketFactory.getDefaultCipherSuites();
    }

    @Override
    public String[] getSupportedCipherSuites() {
        return socketFactory.getSupportedCipherSuites();
    }

    @Override
    public Socket createSocket(Socket socket, String string, int num, boolean bool) throws IOException {
        return socketFactory.createSocket(socket, string, num, bool);
    }

    @Override
    public Socket createSocket(String string, int num) throws IOException, UnknownHostException {
        return socketFactory.createSocket(string, num);
    }

    @Override
    public Socket createSocket(String string, int num, InetAddress netAdd, int i) throws IOException, UnknownHostException {
        return socketFactory.createSocket(string, num, netAdd, i);
    }

    @Override
    public Socket createSocket(InetAddress netAdd, int num) throws IOException {
        return socketFactory.createSocket(netAdd, num);
    }

    @Override
    public Socket createSocket(InetAddress netAdd1, int num, InetAddress netAdd2, int i) throws IOException {
        return socketFactory.createSocket(netAdd1, num, netAdd2, i);
    }

    /**
     * 绕过证书校验
     */
    public static class DummyTrustmanager implements X509TrustManager {
        public void checkClientTrusted(X509Certificate[] cert, String string) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] cert, String string) throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }

    }
}

(2)自定义支持SSL的SSLContextSource

package com.bugdongdong.utils.tools.ldap;

import org.springframework.ldap.core.support.LdapContextSource;
import javax.naming.Context;
import java.util.Hashtable;

public class SSLLdapContextSource extends LdapContextSource {
    public Hashtable<String, Object> getAnonymousEnv(){
        // 禁用jdk8以上对ldap的端点校验
        System.setProperty("com.sun.jndi.ldap.object.disableEndpointIdentification", "true");
        Hashtable<String, Object> anonymousEnv = super.getAnonymousEnv();
        anonymousEnv.put("java.naming.security.protocol", "ssl");
        anonymousEnv.put("java.naming.ldap.factory.socket", CustomSSLSocketFactory.class.getName());
        anonymousEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        return anonymousEnv;
    }
}

(3)构建支持SSL的LdapTemplate

// 普通ldap连接使用普通的Context配置
LdapContextSource contextSource = new LdapContextSource();
String url = "";
if (DataSourceLdapConfig.TRANSPORT_TYPE_CLEAR.equals(config.getTransportType())) {
    url = "ldap://" + config.getServer() + ":" + config.getPort();
} else if (DataSourceLdapConfig.TRANSPORT_TYPE_LDAPS.equals(config.getTransportType())) {
    url = "ldaps://" + config.getServer() + ":" + config.getPort();
    // ldaps使用自定义的支持SSL的Context配置
    contextSource = new SSLLdapContextSource();
}
contextSource.setUserDn(config.getUsername());
contextSource.setPassword(config.getPassword());
contextSource.setUrl(url);
contextSource.setBase(config.getBaseDn());
contextSource.setAnonymousReadOnly(false);
contextSource.setPooled(false);
contextSource.afterPropertiesSet();

this.ldapTemplate = new LdapTemplate(contextSource);
this.ldapTemplate.setIgnorePartialResultException(true);

配置完成后,测试连接即可。

三、问题讨论

需要注意的是,上述有一项配置非常重要,即

System.setProperty("com.sun.jndi.ldap.object.disableEndpointIdentification", "true");

这项配置是JDK8之后需要加上的,官方在JDK8更新后加了端点校验,即使是通过TrustManager绕过了证书校验,有可能还是会因为证书不匹配报错,当然该项配置除了上述这种方式写入,也可以通过JVM参数在程序启动时加入

-Dcom.sun.jndi.ldap.object.disableEndpointIdentification=true

附该项校验使用的源码

以下是官方对该项配置的解释:

Java 8 Update 181 (8u181)

发行版要点说明

IANA Data 2018e

**删除的功能:**删除 Java DB

**更改:**改进 LDAP 支持

参考资料

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文