亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用RestTemplate訪問https實現(xiàn)SSL請求操作

 更新時間:2021年10月29日 11:42:54   作者:一棵樹~  
這篇文章主要介紹了使用RestTemplate訪問https實現(xiàn)SSL請求操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

方法1: 用java生成證書,不建議,移植性差。

方法2: 將RestTemplate改為https請求。

1、添加HttpsClientRequestFactory工具類

import org.springframework.http.client.SimpleClientHttpRequestFactory;
import javax.net.ssl.*;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.Socket;
import java.security.cert.X509Certificate;
 
/**
 * TLS的三個作用:
 * 	(1)身份認證
 * 		通過證書認證來確認對方的身份,防止中間人攻擊
 * 	(2)數(shù)據(jù)私密性
 * 		使用對稱性密鑰加密傳輸?shù)臄?shù)據(jù),由于密鑰只有客戶端/服務(wù)端有,其他人無法窺探。
 * 	(3)數(shù)據(jù)完整性
 * 		使用摘要算法對報文進行計算,收到消息后校驗該值防止數(shù)據(jù)被篡改或丟失。
 *     
 *     使用RestTemplate進行HTTPS請求訪問:
 * 	private static RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());
 * 
 */
public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory {
    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) {
        try {
            if (!(connection instanceof HttpsURLConnection)) {
                throw new RuntimeException("An instance of HttpsURLConnection is expected");
            }
 
            HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; 
            TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        @Override
                        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return null;
                        }
                        @Override
                        public void checkClientTrusted(X509Certificate[] certs, String authType) {
                        }
                        @Override
                        public void checkServerTrusted(X509Certificate[] certs, String authType) {
                        } 
                    }
            };
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            httpsConnection.setSSLSocketFactory(new MyCustomSSLSocketFactory(sslContext.getSocketFactory()));
 
            httpsConnection.setHostnameVerifier(new HostnameVerifier() {
                @Override
                public boolean verify(String s, SSLSession sslSession) {
                    return true;
                }
            });
 
            super.prepareConnection(httpsConnection, httpMethod);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private static class MyCustomSSLSocketFactory extends SSLSocketFactory { 
        private final SSLSocketFactory delegate; 
        public MyCustomSSLSocketFactory(SSLSocketFactory delegate) {
            this.delegate = delegate;
        }
 
        // 返回默認啟用的密碼套件。除非一個列表啟用,對SSL連接的握手會使用這些密碼套件。
        // 這些默認的服務(wù)的最低質(zhì)量要求保密保護和服務(wù)器身份驗證
        @Override
        public String[] getDefaultCipherSuites() {
            return delegate.getDefaultCipherSuites();
        }
 
        // 返回的密碼套件可用于SSL連接啟用的名字
        @Override
        public String[] getSupportedCipherSuites() {
            return delegate.getSupportedCipherSuites();
        } 
 
        @Override
        public Socket createSocket(final Socket socket, final String host, final int port,
                                   final boolean autoClose) throws IOException {
            final Socket underlyingSocket = delegate.createSocket(socket, host, port, autoClose);
            return overrideProtocol(underlyingSocket);
        } 
 
        @Override
        public Socket createSocket(final String host, final int port) throws IOException {
            final Socket underlyingSocket = delegate.createSocket(host, port);
            return overrideProtocol(underlyingSocket);
        }
 
        @Override
        public Socket createSocket(final String host, final int port, final InetAddress localAddress,
                                   final int localPort) throws
                IOException {
            final Socket underlyingSocket = delegate.createSocket(host, port, localAddress, localPort);
            return overrideProtocol(underlyingSocket);
        }
 
        @Override
        public Socket createSocket(final InetAddress host, final int port) throws IOException {
            final Socket underlyingSocket = delegate.createSocket(host, port);
            return overrideProtocol(underlyingSocket);
        }
 
        @Override
        public Socket createSocket(final InetAddress host, final int port, final InetAddress localAddress,
                                   final int localPort) throws
                IOException {
            final Socket underlyingSocket = delegate.createSocket(host, port, localAddress, localPort);
            return overrideProtocol(underlyingSocket);
        }
 
        private Socket overrideProtocol(final Socket socket) {
            if (!(socket instanceof SSLSocket)) {
                throw new RuntimeException("An instance of SSLSocket is expected");
            }
            //((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1.2"});
            ((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1", "TLSv1.1", "TLSv1.2"});
            return socket;
        }
    }
}

注意:服務(wù)端TLS版本要和客戶端工具類中定義的一致。(TLSv1.2)

2、修改RestTemplate

在使用的時候,將

private static RestTemplate restTemplate = new RestTemplate();

改為:

private static RestTemplate restTemplate = new RestTemplate(new HttpsClientRequestFactory());

其他代碼不變。

也可使用注入的方式:

@Configuration
public class ConfigBean {
 @Bean
 public RestTemplate getRestTemplate() {
  return new RestTemplate(new HttpsClientRequestFactory());
 }
}

3、訪問https,拋出的異常

javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure解決方案

因為jdk中jce的安全機制導(dǎo)致報的錯,需要去oracle官網(wǎng)下載對應(yīng)的jce包替換jdk中的jce包。

方案一:替換jce包

目錄 %JAVA_HOME%\jre\lib\security里的local_policy.jar,US_export_policy.jar
JDK7 http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
JDK8 http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
 
// pub1:/home/myron/jdk1.7.0_80 % cd $JAVA_HOME/jre/lib/security/  //jce所在jdk的路徑
US_export_policy.jar
local_policy.jar

方案二:升級 JDK到1.8版本(推薦方式)

// pub1:/home/myron % vi .cshrc
setenv JAVA_HOME /home/myron/jdk1.8.0_211
// pub1:/home/myron % source .cshrc
// pub1:/home/myron % java -version
java version "1.8.0_211"

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

相關(guān)文章

  • SpringBoot公共頁面抽取方法實現(xiàn)過程介紹

    SpringBoot公共頁面抽取方法實現(xiàn)過程介紹

    這篇文章主要介紹了SpringBoot抽取公共頁面的方法實現(xiàn)過程,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-10-10
  • Java fastjson解析json字符串實現(xiàn)過程解析

    Java fastjson解析json字符串實現(xiàn)過程解析

    這篇文章主要介紹了Java fastjson解析json字符串實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-10-10
  • Mybatis-Plus雪花id的使用以及解析機器ID和數(shù)據(jù)標(biāo)識ID實現(xiàn)

    Mybatis-Plus雪花id的使用以及解析機器ID和數(shù)據(jù)標(biāo)識ID實現(xiàn)

    這篇文章主要介紹了Mybatis-Plus雪花id的使用以及解析機器ID和數(shù)據(jù)標(biāo)識ID實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • java實現(xiàn)上傳圖片并壓縮圖片大小功能

    java實現(xiàn)上傳圖片并壓縮圖片大小功能

    這篇文章主要為大家詳細介紹了java實現(xiàn)上傳圖片并壓縮圖片大小功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • Intellij IDEA十大快捷鍵

    Intellij IDEA十大快捷鍵

    Intellij IDEA中有很多快捷鍵讓人愛不釋手,stackoverflow上也有一些有趣的討論.這篇文章主要介紹了Intellij IDEA十大快捷鍵,需要的朋友可以參考下
    2018-03-03
  • SpringBoot配置加載,各配置文件優(yōu)先級對比方式

    SpringBoot配置加載,各配置文件優(yōu)先級對比方式

    這篇文章主要介紹了SpringBoot配置加載,各配置文件優(yōu)先級對比方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • SpringBoot如何動態(tài)改變?nèi)罩炯墑e

    SpringBoot如何動態(tài)改變?nèi)罩炯墑e

    這篇文章主要介紹了SpringBoot如何動態(tài)改變?nèi)罩炯墑e,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下
    2020-12-12
  • 使用Spring CROS解決項目中的跨域問題詳解

    使用Spring CROS解決項目中的跨域問題詳解

    這篇文章主要介紹了使用Spring CROS解決項目中的跨域問題詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實現(xiàn)

    java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實現(xiàn)

    這篇文章主要介紹了java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java觀察者設(shè)計模式(Observable和Observer)

    Java觀察者設(shè)計模式(Observable和Observer)

    這篇文章主要介紹了 Java觀察者設(shè)計模式(Observable和Observer)的相關(guān)資料,需要的朋友可以參考下
    2015-12-12

最新評論