SpringLDAP連接LDAPS證書報(bào)錯(cuò)問題及解決
一、問題背景
Java操作LDAP一般通過Spring LDAP比較方便,一般我們都是使用的常規(guī)的非加密的389端口
常規(guī)的初始化如下:
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);
但是最近遇到一個(gè)使用證書加密環(huán)境的LDAP,即LDAPS(LDAP+SSL),使用的是636端口,再使用上述的配置,則會(huì)報(bào)錯(cuò),可能會(huì)報(bào)以下的未找到合法證書的錯(cuò)誤:
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]
二、解決方案
一般我們?cè)贘ava使用HTTPS客戶端的時(shí)候?yàn)榱吮苊庾C書報(bào)錯(cuò),一般會(huì)將客戶端證書導(dǎo)入到JDK中,但是有些環(huán)境的證書是自簽名的證書,導(dǎo)入也不一定能解決問題。
因此多數(shù)也會(huì)通過X509TrustManager和SSLSocketFactory繞過證書校驗(yàn),所以我們對(duì)于LDAPS也采用同樣的思路來解決,網(wǎng)上有類似的解決方案,但是集成之后可能還是存在以下的報(bào)錯(cuò):
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)
我的解決方案分為以下幾個(gè)步驟,能規(guī)避以上錯(cuò)誤:
(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);
}
/**
* 繞過證書校驗(yàn)
*/
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以上對(duì)ldap的端點(diǎn)校驗(yàn)
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)構(gòu)建支持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);
配置完成后,測(cè)試連接即可。
三、問題討論
需要注意的是,上述有一項(xiàng)配置非常重要,即
System.setProperty("com.sun.jndi.ldap.object.disableEndpointIdentification", "true");這項(xiàng)配置是JDK8之后需要加上的,官方在JDK8更新后加了端點(diǎn)校驗(yàn),即使是通過TrustManager繞過了證書校驗(yàn),有可能還是會(huì)因?yàn)樽C書不匹配報(bào)錯(cuò),當(dāng)然該項(xiàng)配置除了上述這種方式寫入,也可以通過JVM參數(shù)在程序啟動(dòng)時(shí)加入
-Dcom.sun.jndi.ldap.object.disableEndpointIdentification=true
附該項(xiàng)校驗(yàn)使用的源碼


以下是官方對(duì)該項(xiàng)配置的解釋:
Java 8 Update 181 (8u181)
發(fā)行版要點(diǎn)說明
IANA Data 2018e
- JDK 8u181 包含 IANA 時(shí)區(qū)數(shù)據(jù)版本 2018e。
- 有關(guān)詳細(xì)信息,請(qǐng)參閱 JRE 軟件中的時(shí)區(qū)數(shù)據(jù)版本。
**刪除的功能:**刪除 Java DB
- Java DB 也稱為 Apache Derby,已在本發(fā)行版中刪除。
- 建議您直接從以下網(wǎng)址的 Apache 項(xiàng)目獲取最新的 Apache Derby:
- https://db.apache.org/derby
- JDK-8197871(非公共)
**更改:**改進(jìn) LDAP 支持
- 已在 LDAPS 連接上啟用端點(diǎn)識(shí)別。
- 為提高 LDAPS(TLS 上的安全 LDAP)連接的強(qiáng)健性,默認(rèn)情況下已啟用端點(diǎn)識(shí)別算法。
- 請(qǐng)注意,可能在一些情況下,以前能夠成功連接到 LDAPS 服務(wù)器的一些應(yīng)用程序可能不再能夠成功連接。如果此類應(yīng)用程序認(rèn)為合適的話,它們可能會(huì)使用新系統(tǒng)屬性禁用端點(diǎn)識(shí)別:com.sun.jndi.ldap.object.disableEndpointIdentification。
- 定義此系統(tǒng)屬性(或者將它設(shè)置為 true)可禁用端點(diǎn)識(shí)別算法。
參考資料
- 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
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- springboot操作ldap全過程
- springboot配置ldaps連接方式
- SpringBoot整合Ldap的實(shí)現(xiàn)示例
- Spring Security LDAP實(shí)現(xiàn)身份驗(yàn)證的項(xiàng)目實(shí)踐
- SpringBoot整合LDAP的流程分析
- 淺談Spring Security LDAP簡(jiǎn)介
- Vue+Jwt+SpringBoot+Ldap完成登錄認(rèn)證的示例代碼
- Spring Boot中使用LDAP來統(tǒng)一管理用戶信息的示例
- Spring Boot 連接LDAP的方法
- Spring LDAP目錄服務(wù)的使用示例
相關(guān)文章
實(shí)現(xiàn)分布式WebSocket集群的方法
本文總結(jié)出了幾個(gè)實(shí)現(xiàn)分布式WebSocket集群的辦法,從zuul到spring cloud gateway的不同嘗試,總結(jié)出了這篇文章,希望能幫助到某些人,并且能一起分享這方面的想法與研究2022-03-03
IDEA查看所有的斷點(diǎn)(Breakpoints)并關(guān)閉的方式
我們?cè)谑褂肐DEA開發(fā)Java應(yīng)用時(shí),基本上都需要進(jìn)行打斷點(diǎn)的操作,這方便我們排查BUG,也方便我們查看設(shè)計(jì)的是否正確,不過有時(shí)候,我們不希望進(jìn)入斷點(diǎn),所以我們需要快速關(guān)閉所有斷點(diǎn),故本文給大家介紹了IDEA查看所有的斷點(diǎn)(Breakpoints)并關(guān)閉的方式2024-10-10
詳解配置spring-boot-actuator時(shí)候遇到的一些小問題
這篇文章主要介紹了詳解配置spring-boot-actuator時(shí)候遇到的一些小問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
IntelliJ?IDEA的代碼擱置功能實(shí)現(xiàn)
本文主要介紹了IntelliJ?IDEA的代碼擱置功能實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
Mybatis實(shí)現(xiàn)動(dòng)態(tài)建表代碼實(shí)例
這篇文章主要介紹了Mybatis實(shí)現(xiàn)動(dòng)態(tài)建表代碼實(shí)例,解釋一下,就是指根據(jù)傳入的表名,動(dòng)態(tài)地創(chuàng)建數(shù)據(jù)庫表,以供后面的業(yè)務(wù)場(chǎng)景使用,2023-10-10
而使用 Mybatis 的動(dòng)態(tài) SQL,就能很好地為我們解決這個(gè)問題,需要的朋友可以參考下

