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

springboot配置ldaps連接方式

 更新時(shí)間:2024年05月17日 16:17:28   作者:天道有情戰(zhàn)天下  
這篇文章主要介紹了springboot配置ldaps連接方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

springboot配置ldaps連接

spring boot配置ldap 連接時(shí),通過ldap://xxxxx:389 連接,一般來說都能成功,但是如果配置ldap ssl 連接,ldaps://xxxx:636 那么很大概率會(huì)出現(xiàn)

javax.naming.CommunicationException: simple bind failed: xxxxxtest.com.local:636

這種異常 。

百度,谷歌搜索 大部分解決方案是需要從ldap 服務(wù)器上導(dǎo)出證書,然后再通過Java的keytool 工具導(dǎo)入證書,比較繁瑣,我也沒試過好不好使,反正從服務(wù)器上導(dǎo)出證書那一步就很煩了。

說一下如何代碼配置ldap跳過ssl

直接上代碼。

package com.github.wxiaoqi.security.common.config;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.support.LdapContextSource;
 
import java.util.Hashtable;
import java.util.Objects;
 
/**
 * @author margo
 * @date 2021/11/4
 */
@Slf4j
// @ConditionalOnExpression("${ldap.enabled:false}")
public class LdapConfiguration {
 
    private LdapTemplate ldapTemplate;
 
    @Value("${ldap.url}")
    private String ldapUrl;
 
    @Value("${ldap.basedc}")
    private String ldapBaseDc;
 
    @Value("${ldap.username}")
    private String ldapUsername;
 
    @Value("${ldap.passwd}")
    private String ldapPasswd;
 
 
    /**
     * 繼承LdapContextSource重寫getAnonymousEnv方法來加載,
     * 使連接ldap時(shí)用SSL連接(由于修改AD密碼時(shí)必須使用SSL連接)
     */
    public class SsldapContextSource extends LdapContextSource {
        @Override
        public Hashtable<String, Object> getAnonymousEnv(){
            Hashtable<String, Object> anonymousEnv = super.getAnonymousEnv();
            anonymousEnv.put("java.naming.security.protocol", "ssl");
            anonymousEnv.put("java.naming.ldap.factory.socket", CustomSslSocketFactory.class.getName());
            return anonymousEnv;
        }
    }
 
 
    @Bean
    public LdapContextSource contextSource() {
 
        SsldapContextSource ldapContextSource = new SsldapContextSource();
        ldapContextSource.setBase(ldapBaseDc);
        ldapContextSource.setUrl(ldapUrl);
        ldapContextSource.setUserDn(ldapUsername);
        ldapContextSource.setPassword(ldapPasswd);
        ldapContextSource.setPooled(false);
        ldapContextSource.setReferral("follow");
        ldapContextSource.afterPropertiesSet();
        return ldapContextSource;
    }
 
    @Bean
    public LdapTemplate ldapTemplate(LdapContextSource contextSource) {
        if (Objects.isNull(contextSource)) {
            throw new RuntimeException("ldap contextSource error");
        }
        if (null == ldapTemplate) {
            ldapTemplate = new LdapTemplate(contextSource);
        }
        return ldapTemplate;
    }
 
}
package com.github.wxiaoqi.security.common.config;
 
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;
 
/**
 * 自定義的SSL工廠里面加載自己實(shí)現(xiàn)X509TrustManager,信任自簽證書
 * @author cb
 */
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 {
        @Override
        public void checkClientTrusted(X509Certificate[] cert, String string) throws CertificateException {
        }
 
        @Override
        public void checkServerTrusted(X509Certificate[] cert, String string) throws CertificateException {
        }
 
        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[0];
        }
 
    }
}

主要的配置是 CustomSslSocketFactory  這個(gè)類,其他的正常配置。

配置好后啟動(dòng)應(yīng)用,又出現(xiàn)了另外一個(gè)錯(cuò)誤,

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException

在啟動(dòng)main方法中加上一行環(huán)境變量配置即可

@EnableEurekaClient
@SpringBootApplication
@EnableConfigurationProperties
@EnableTransactionManagement
@Import(value = {RedissonConfig.class, GatewayReqInterceptor.class, UserInfoInterceptor.class, InterceptorConfig.class, CoreConfig.class, AuthConfig.class, AuthServerRunner.class, LdapConfiguration.class})
@EnableScheduling
public class WxCpApplication {
 
    public static void main(String[] args) {
        System.setProperty("com.sun.jndi.ldap.object.disableEndpointIdentification", "true");  // important resolve javax.net.ssl.SSLHandshakeException
        SpringApplication.run(WxCpApplication.class, args);
    }
 
    
}
System.setProperty("com.sun.jndi.ldap.object.disableEndpointIdentification", "true");  這行

總結(jié)

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

相關(guān)文章

  • 如何用idea數(shù)據(jù)庫編寫快遞e站

    如何用idea數(shù)據(jù)庫編寫快遞e站

    這篇文章主要介紹了如何用idea數(shù)據(jù)庫編寫快遞e站,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • Idea報(bào)錯(cuò): A JNI error has occurred的問題

    Idea報(bào)錯(cuò): A JNI error has occurred的問題

    這篇文章主要介紹了Idea報(bào)錯(cuò): A JNI error has occurred的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 深入理解final變量的初始化

    深入理解final變量的初始化

    本篇文章是對(duì)final變量的初始化進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • 詳解Spring Boot 定時(shí)任務(wù)的實(shí)現(xiàn)方法

    詳解Spring Boot 定時(shí)任務(wù)的實(shí)現(xiàn)方法

    最近在用SpringBoot寫一個(gè)關(guān)于定時(shí)項(xiàng)目的時(shí)候遇到一個(gè)問題,下面小編把如何處理定時(shí)任務(wù)的解決思路分享給大家 ,需要的朋友參考下
    2017-05-05
  • 解決SpringBoot啟動(dòng)報(bào)錯(cuò):Failed?to?load?property?source?from?location?'classpath:/application.yml'問題

    解決SpringBoot啟動(dòng)報(bào)錯(cuò):Failed?to?load?property?source?from?l

    這篇文章主要介紹了解決SpringBoot啟動(dòng)報(bào)錯(cuò):Failed?to?load?property?source?from?location?'classpath:/application.yml'問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • SpringMVC基于注解的Controller詳解

    SpringMVC基于注解的Controller詳解

    這篇文章主要介紹了SpringMVC基于注解的Controller詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • 使用JWT作為Spring?Security?OAuth2的token存儲(chǔ)問題

    使用JWT作為Spring?Security?OAuth2的token存儲(chǔ)問題

    這篇文章主要介紹了使用JWT作為Spring?Security?OAuth2的token存儲(chǔ),大家經(jīng)常使用的方法有兩種一種是使用JWT作為Token傳遞,一種是使用Redis存儲(chǔ)Token,資源服務(wù)器本地訪問Redis校驗(yàn)Token,需要的朋友可以參考下
    2021-12-12
  • JAVA獲取rabbitmq消息總數(shù)過程詳解

    JAVA獲取rabbitmq消息總數(shù)過程詳解

    這篇文章主要介紹了JAVA獲取rabbitmq消息總數(shù)過程詳解,公司使用的是rabbitMQ,需要做監(jiān)控預(yù)警的job去監(jiān)控rabbitMQ里面的堆積消息個(gè)數(shù),如何使用rabbitMQ獲取監(jiān)控的隊(duì)列里面的隊(duì)列消息個(gè)數(shù)呢,需要的朋友可以參考下
    2019-07-07
  • Java實(shí)現(xiàn)圖片與二進(jìn)制的互相轉(zhuǎn)換

    Java實(shí)現(xiàn)圖片與二進(jìn)制的互相轉(zhuǎn)換

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖片與二進(jìn)制的互相轉(zhuǎn)換,將圖片轉(zhuǎn)二進(jìn)制再將二進(jìn)制轉(zhuǎn)成圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • JAVA多線程線程安全性基礎(chǔ)

    JAVA多線程線程安全性基礎(chǔ)

    這篇文章主要介紹了如何測(cè)試Java類的線程安全性,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-08-08

最新評(píng)論

遂溪县| 安陆市| 依兰县| 商城县| 林州市| 嘉鱼县| 淮北市| 利辛县| 林州市| 突泉县| 沾益县| 砀山县| 津南区| 团风县| 八宿县| 洛阳市| 石渠县| 永州市| 陆川县| 贞丰县| 宣武区| 青河县| 河东区| 定襄县| 阿城市| 榆社县| 博乐市| 邯郸市| 曲松县| 永宁县| 象州县| 河曲县| 吉首市| 儋州市| 容城县| 古蔺县| 翁牛特旗| 松桃| 景德镇市| 桐乡市| 右玉县|