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

Java跳過(guò)證書(shū)訪問(wèn)HTTPS詳細(xì)代碼示例

 更新時(shí)間:2024年02月03日 11:15:14   作者:李奈?-?Leemon  
在訪問(wèn)HTTPS網(wǎng)站時(shí),Java會(huì)默認(rèn)檢查SSL證書(shū)是否有效,如果證書(shū)無(wú)效,則會(huì)阻止訪問(wèn),這篇文章主要給大家介紹了關(guān)于Java跳過(guò)證書(shū)訪問(wèn)HTTPS的相關(guān)資料,需要的朋友可以參考下

前言

java直接發(fā)送請(qǐng)求訪問(wèn)https地址的時(shí)候,若沒(méi)有導(dǎo)入證書(shū),會(huì)出現(xiàn)各種問(wèn)題,如307。

以下會(huì)以是否SpringBoot來(lái)解決這個(gè)問(wèn)題,做法一致,都是繞過(guò)證書(shū)進(jìn)行處理的。

一,非Spring方式

創(chuàng)建一個(gè)請(qǐng)求代理類(lèi),為所有的HTTPS請(qǐng)求訪問(wèn)前做一下操作

public class IgnoreHttpsProxyRequest {

	/**
	 * 通過(guò)HTTPS的url登錄
	 * @param urlStr 目標(biāo)url
	 * @return	查詢(xún)結(jié)果
	 * @throws IOException
	 * @throws NoSuchAlgorithmException
	 * @throws KeyManagementException
	 */
	public String get(String urlStr, String token, String type) throws IOException, NoSuchAlgorithmException, KeyManagementException {
		//繞過(guò)https
		HttpsURLConnection.setDefaultHostnameVerifier(new IgnoreHttpsProxyRequest().new NullHostNameVerifier());
		SSLContext sslContext = SSLContext.getInstance("TLS");
		sslContext.init(null, trustManagers, new SecureRandom());
		HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
		//建立連接
		URL url = new URL(urlStr);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.setRequestMethod(type);
		connection.setRequestProperty(HttpHeaders.AUTHORIZATION, "Bearer " + token);
		connection.connect();
		//獲取查詢(xún)結(jié)果
		InputStream inputStream = connection.getInputStream();
		if (inputStream == null) {
			return null;
		}
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
		StringBuilder sb = new StringBuilder();
		String tmp = null;
		while ((tmp = bufferedReader.readLine()) != null) {
			sb.append(tmp);
		}
		bufferedReader.close();
		inputStream.close();
		return sb.toString();

	}

	static TrustManager[] trustManagers = new TrustManager[] {
				new X509TrustManager() {
					@Override
					public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

					}

					@Override
					public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

					}

					@Override
					public X509Certificate[] getAcceptedIssuers() {
						return null;
					}
				}
		};

    public class NullHostNameVerifier implements HostnameVerifier {

		@Override
		public boolean verify(String s, SSLSession sslSession) {
			return true;
		}
	}

}

二,SpringBoot方式

先創(chuàng)建一個(gè)跳過(guò)證書(shū)驗(yàn)證,信任所有站點(diǎn)的請(qǐng)求客戶端factory

package com.foxconn.dsc.matrix.api;

import org.springframework.http.client.SimpleClientHttpRequestFactory;

import javax.net.ssl.*;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

/**
 * @ClassName: SkipHttpsRequestFactory
 * @Description:
 * @author: lemon
 * @date: 2023/9/14 13:56
 */
public class SkipHttpsRequestFactory extends SimpleClientHttpRequestFactory {

    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
        if (connection instanceof HttpsURLConnection) {
            prepareHttpsConnection((HttpsURLConnection) connection);
        }
        super.prepareConnection(connection, httpMethod);
    }

    private void prepareHttpsConnection(HttpsURLConnection connection) {
        connection.setHostnameVerifier(new SkipHostnameVerifier());
        try {
            connection.setSSLSocketFactory(createSslSocketFactory());
        } catch (Exception ex) {
            // Ignore
        }
    }

    private SSLSocketFactory createSslSocketFactory() throws Exception {
        SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new SkipX509TrustManager() }, new SecureRandom());
        return context.getSocketFactory();
    }

    private class SkipHostnameVerifier implements HostnameVerifier {

        @Override
        public boolean verify(String s, SSLSession sslSession) {
            return true;
        }

    }

    private static class SkipX509TrustManager implements X509TrustManager {

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

        @Override
        public void checkClientTrusted(X509Certificate[] chain, String authType) {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] chain, String authType) {
        }
    }
}

注入RestTemplate類(lèi)時(shí),構(gòu)造時(shí)將該工廠類(lèi)加上。

    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory factory = new SkipHttpsRequestFactory();
        RestTemplate restTemplate = new RestTemplate(factory);
        return restTemplate;
    }

使用時(shí)將其注入

@Resource
private RestTemplate restTemplate;

配置完畢之后,就可以直接調(diào)用了

ResponseEntity<String> response = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, entity, String.class);

總結(jié)

到此這篇關(guān)于Java跳過(guò)證書(shū)訪問(wèn)HTTPS的文章就介紹到這了,更多相關(guān)Java跳過(guò)證書(shū)訪問(wèn)HTTPS內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringCloud之網(wǎng)關(guān)、服務(wù)保護(hù)和分布式事務(wù)詳解

    SpringCloud之網(wǎng)關(guān)、服務(wù)保護(hù)和分布式事務(wù)詳解

    網(wǎng)關(guān)負(fù)責(zé)路由、身份驗(yàn)證及用戶ID傳遞,配置管理支持熱更新與動(dòng)態(tài)路由,服務(wù)保護(hù)通過(guò)限流、隔離、熔斷防止雪崩,分布式事務(wù)采用XA模式強(qiáng)一致性或AT模式高性能但控制復(fù)雜
    2025-09-09
  • SpringBoot2種單元測(cè)試方法解析

    SpringBoot2種單元測(cè)試方法解析

    這篇文章主要介紹了SpringBoot2種單元測(cè)試方法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Springboot?配置SqlSessionFactory方式

    Springboot?配置SqlSessionFactory方式

    這篇文章主要介紹了Springboot?配置SqlSessionFactory方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Java整型數(shù)與網(wǎng)絡(luò)字節(jié)序byte[]數(shù)組轉(zhuǎn)換關(guān)系詳解

    Java整型數(shù)與網(wǎng)絡(luò)字節(jié)序byte[]數(shù)組轉(zhuǎn)換關(guān)系詳解

    這篇文章主要介紹了Java整型數(shù)與網(wǎng)絡(luò)字節(jié)序byte[]數(shù)組轉(zhuǎn)換關(guān)系,結(jié)合實(shí)例形式歸納整理了java整型數(shù)和網(wǎng)絡(luò)字節(jié)序的byte[]之間轉(zhuǎn)換的各種情況,需要的朋友可以參考下
    2017-08-08
  • logback的FileAppender文件追加模式和沖突檢測(cè)解讀

    logback的FileAppender文件追加模式和沖突檢測(cè)解讀

    這篇文章主要為大家介紹了logback的FileAppender文件追加模式和沖突檢測(cè)解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • Maven工程搭建spring boot+spring mvc+JPA的示例

    Maven工程搭建spring boot+spring mvc+JPA的示例

    本篇文章主要介紹了Maven工程搭建spring boot+spring mvc+JPA的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • 淺談Java編程中的單例設(shè)計(jì)模式

    淺談Java編程中的單例設(shè)計(jì)模式

    這篇文章主要介紹了Java編程中的單例設(shè)計(jì)模式,在許多語(yǔ)言的編程過(guò)程當(dāng)中單例模式都被開(kāi)發(fā)者們廣泛采用,需要的朋友可以參考下
    2015-07-07
  • Java多線程處理文件的示例詳解

    Java多線程處理文件的示例詳解

    在Java編程中,文件處理是一項(xiàng)常見(jiàn)的任務(wù),為了提高文件處理的效率,我們可以使用多線程技術(shù),本文將詳細(xì)介紹如何使用Java多線程來(lái)處理文件,需要的可以參考下
    2024-12-12
  • Spring?invokeBeanFactoryPostProcessors方法刨析源碼

    Spring?invokeBeanFactoryPostProcessors方法刨析源碼

    invokeBeanFactoryPostProcessors該方法會(huì)實(shí)例化所有BeanDefinitionRegistryPostProcessor和BeanFactoryPostProcessor的實(shí)例并且執(zhí)行postProcessBeanFactory與postProcessBeanDefinitionRegistry方法
    2023-01-01
  • Java Stream流知識(shí)總結(jié)

    Java Stream流知識(shí)總結(jié)

    這篇文章主要介紹了Java Stream流的相關(guān)知識(shí),文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06

最新評(píng)論

陵水| 辽源市| 平顺县| 榆林市| 延庆县| 宜章县| 鹰潭市| 沂南县| 吉林省| 潮州市| 应用必备| 和硕县| 襄汾县| 舞阳县| 长寿区| 富源县| 平原县| 衡阳市| 泰安市| 北海市| 永嘉县| 屏东县| 嘉祥县| 澎湖县| 伊金霍洛旗| 恩施市| 永仁县| 阿克陶县| 田阳县| 吴堡县| 凌源市| 平度市| 麻栗坡县| 东方市| 汶上县| 临安市| 永丰县| 五台县| 迁安市| 呈贡县| 松阳县|