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
    JDK 8u181 包含 IANA 时区数据版本 2018e。有关详细信息,请参阅 JRE 软件中的时区数据版本
  • **删除的功能:**删除 Java DB
    Java DB 也称为 Apache Derby,已在本发行版中删除。
    建议您直接从以下网址的 Apache 项目获取最新的 Apache Derby:
    https://db.apache.org/derby
    JDK-8197871(非公共)
  • **更改:**改进 LDAP 支持
    已在 LDAPS 连接上启用端点识别。
    为提高 LDAPS(TLS 上的安全 LDAP)连接的强健性,默认情况下已启用端点识别算法。
    请注意,可能在一些情况下,以前能够成功连接到 LDAPS 服务器的一些应用程序可能不再能够成功连接。如果此类应用程序认为合适的话,它们可能会使用新系统属性禁用端点识别:com.sun.jndi.ldap.object.disableEndpointIdentification
    定义此系统属性(或者将它设置为 true)可禁用端点识别算法。

四、参考资料

https://stackoverflow.com/questions/30546193/spring-ldapcontextsource-ignores-sslsocketfactory/30573130
https://docs.spring.io/spring-ldap/docs/2.3.3.RELEASE/reference/
https://java.com/zh-CN/download/help/release_changes.html

  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
好的,以下是一个 Spring Boot 集成 LDAPS 的示例代码: 首先,我们需要在 pom.xml 文件中添加 ldap 相关的依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-ldap</artifactId> </dependency> ``` 接下来,在 application.properties 文件中配置 LDAPS 连接信息,如下所示: ``` spring.ldap.urls=ldaps://your-ldap-server:636 spring.ldap.username=your-ldap-username spring.ldap.password=your-ldap-password spring.ldap.base=your-ldap-base-dn spring.ldap.user-dn-pattern=your-ldap-user-dn-pattern spring.ldap.user-search-filter=your-ldap-user-search-filter ``` 然后,我们创建一个 LDAP 配置类,用于将上述配置信息注入到 LdapTemplate 中: ``` @Configuration public class LdapConfiguration { @Value("${spring.ldap.urls}") private String url; @Value("${spring.ldap.username}") private String username; @Value("${spring.ldap.password}") private String password; @Value("${spring.ldap.base}") private String base; @Value("${spring.ldap.user-dn-pattern}") private String userDnPattern; @Value("${spring.ldap.user-search-filter}") private String userSearchFilter; @Bean public LdapTemplate ldapTemplate() { LdapTemplate ldapTemplate = new LdapTemplate(); ldapTemplate.setContextSource(contextSource()); return ldapTemplate; } @Bean public LdapContextSource contextSource() { LdapContextSource contextSource = new LdapContextSource(); contextSource.setUrl(url); contextSource.setUserDn(username); contextSource.setPassword(password); contextSource.setBase(base); contextSource.setUserDnPatterns(new String[] {userDnPattern}); contextSource.setAuthenticationSource(new AuthenticationSource() { @Override public DirContext getDirContext(String principal, String credentials) { return contextSource.getContext(principal, credentials); } }); contextSource.afterPropertiesSet(); return contextSource; } @Bean public LdapUserSearch ldapUserSearch() { return new FilterBasedLdapUserSearch("", userSearchFilter, contextSource()); } } ``` 最后,我们可以使用 LdapTemplate 执行 LDAP 操作,例如: ``` @Autowired private LdapTemplate ldapTemplate; public void search() { List<String> result = ldapTemplate.search( LdapQueryBuilder.query().where("objectclass").is("person"), new AttributesMapper<String>() { @Override public String mapFromAttributes(Attributes attributes) throws NamingException { return attributes.get("cn").get().toString(); } }); } ``` 这就是一个 Spring Boot 集成 LDAPS 的示例。当然,具体的配置信息和 LDAP 操作需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值