最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringLDAP連接LDAPS證書報(bào)錯(cuò)問(wèn)題及解決

 更新時(shí)間:2024年05月20日 10:48:17   作者:初心繪流年  
這篇文章主要介紹了SpringLDAP連接LDAPS證書報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

一、問(wèn)題背景

Java操作LDAP一般通過(guò)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)入也不一定能解決問(wèn)題。

因此多數(shù)也會(huì)通過(guò)X509TrustManager和SSLSocketFactory繞過(guò)證書校驗(yàn),所以我們對(duì)于LDAPS也采用同樣的思路來(lái)解決,網(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);
    }

    /**
     * 繞過(guò)證書校驗(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è)試連接即可。

三、問(wèn)題討論

需要注意的是,上述有一項(xiàng)配置非常重要,即

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

這項(xiàng)配置是JDK8之后需要加上的,官方在JDK8更新后加了端點(diǎn)校驗(yàn),即使是通過(guò)TrustManager繞過(guò)了證書校驗(yàn),有可能還是會(huì)因?yàn)樽C書不匹配報(bào)錯(cuò),當(dāng)然該項(xiàng)配置除了上述這種方式寫入,也可以通過(guò)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)說(shuō)明

IANA Data 2018e

**刪除的功能:**刪除 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í)別算法。

參考資料

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot實(shí)現(xiàn)列表數(shù)據(jù)導(dǎo)出為Excel文件

    SpringBoot實(shí)現(xiàn)列表數(shù)據(jù)導(dǎo)出為Excel文件

    這篇文章主要為大家詳細(xì)介紹了在Spring?Boot框架中如何將列表數(shù)據(jù)導(dǎo)出為Excel文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下
    2024-02-02
  • java拋出異常后,后續(xù)代碼是否繼續(xù)執(zhí)行詳解

    java拋出異常后,后續(xù)代碼是否繼續(xù)執(zhí)行詳解

    這篇文章主要給大家介紹了關(guān)于java拋出異常后,后續(xù)代碼是否繼續(xù)執(zhí)行詳?shù)南嚓P(guān)資料,在Java編程中,異常是當(dāng)程序執(zhí)行時(shí)遇到問(wèn)題時(shí)拋出的一種特殊情況,需要的朋友可以參考下
    2023-07-07
  • Java常用集合之Set和Map的用法詳解

    Java常用集合之Set和Map的用法詳解

    這篇文章將通過(guò)一些示例為大家詳細(xì)介紹一下Java常用集合中Set和Map的用法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-07-07
  • SpringBoot注入自定義的配置文件的方法詳解

    SpringBoot注入自定義的配置文件的方法詳解

    在實(shí)際的項(xiàng)目開(kāi)發(fā)過(guò)程中,我們經(jīng)常需要將某些變量從代碼里面抽離出來(lái),放在配置文件里面。今天,我們就一起來(lái)聊一聊SpringBoot加載配置文件的幾種玩法,需要的可以參考一下
    2022-09-09
  • JSP頁(yè)面pageEncoding和contentType屬性

    JSP頁(yè)面pageEncoding和contentType屬性

    有關(guān)于JSP頁(yè)面中pageEncoding和contentType屬性。
    2013-04-04
  • SpringBoot自定義加載yml實(shí)現(xiàn)方式,附源碼解讀

    SpringBoot自定義加載yml實(shí)現(xiàn)方式,附源碼解讀

    這篇文章主要介紹了SpringBoot自定義加載yml實(shí)現(xiàn)方式附源碼解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • java數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn)

    java數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn)

    本文主要介紹了java數(shù)據(jù)庫(kù)批量插入數(shù)據(jù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • NetBeans安裝提示neatbeans cannot find java 1.8 or higher

    NetBeans安裝提示neatbeans cannot find java 1.8 or higher

    今天小編就為大家分享一篇關(guān)于NetBeans安裝提示neatbeans cannot find java 1.8 or higher,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • mybatisplus駝峰命名映射的問(wèn)題解決

    mybatisplus駝峰命名映射的問(wèn)題解決

    本文主要介紹了mybatisplus駝峰命名映射的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • SpringBoot中SmartLifecycle的使用解析

    SpringBoot中SmartLifecycle的使用解析

    這篇文章主要介紹了SpringBoot中SmartLifecycle的使用解析,SmartLifecycle是一個(gè)擴(kuò)展了Lifecycle接口,可以跟蹤spring容器ApplicationContext刷新或者關(guān)閉的接口,實(shí)現(xiàn)該接口的實(shí)現(xiàn)類有特定的執(zhí)行順序,需要的朋友可以參考下
    2023-11-11

最新評(píng)論

鄯善县| 文昌市| 电白县| 兴山县| 潮州市| 龙里县| 辽宁省| 阳信县| 广水市| 堆龙德庆县| 彰武县| 伊川县| 东乌珠穆沁旗| 西昌市| 浦县| 茂名市| 石景山区| 呼伦贝尔市| 中卫市| 西昌市| 邹平县| 正宁县| 荔波县| 横山县| 同德县| 台山市| 龙江县| 福海县| 策勒县| 阳信县| 南皮县| 芒康县| 安泽县| 犍为县| 铜川市| 马鞍山市| 和静县| 顺义区| 金坛市| 略阳县| 铜山县|