使用RestTemplate訪問https實(shí)現(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的三個(gè)作用:
* (1)身份認(rèn)證
* 通過證書認(rèn)證來確認(rèn)對方的身份,防止中間人攻擊
* (2)數(shù)據(jù)私密性
* 使用對稱性密鑰加密傳輸?shù)臄?shù)據(jù),由于密鑰只有客戶端/服務(wù)端有,其他人無法窺探。
* (3)數(shù)據(jù)完整性
* 使用摘要算法對報(bào)文進(jìn)行計(jì)算,收到消息后校驗(yàn)該值防止數(shù)據(jù)被篡改或丟失。
*
* 使用RestTemplate進(jìn)行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;
}
// 返回默認(rèn)啟用的密碼套件。除非一個(gè)列表啟用,對SSL連接的握手會使用這些密碼套件。
// 這些默認(rèn)的服務(wù)的最低質(zhì)量要求保密保護(hù)和服務(wù)器身份驗(yàn)證
@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
在使用的時(shí)候,將
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解決方案
因?yàn)閖dk中jce的安全機(jī)制導(dǎo)致報(bà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"
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中基于maven實(shí)現(xiàn)zxing二維碼功能
這篇文章主要介紹了Java中基于maven實(shí)現(xiàn)zxing二維碼功能,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
使用Aop的方式實(shí)現(xiàn)自動日志記錄的方式詳細(xì)介紹
這篇文章主要介紹了使用Aop的方式實(shí)現(xiàn)自動日志記錄,通過監(jiān)聽器去監(jiān)聽,當(dāng)訪問到具體的類方法,通過aop切面去獲取訪問的方法,然后將日志記錄下來,就這種方式給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Java 實(shí)現(xiàn)線程池任務(wù)編排的示例代碼
任務(wù)編排是將多個(gè)任務(wù)按照特定的依賴關(guān)系和執(zhí)行順序進(jìn)行組織和管理的過程,以確保任務(wù)能按預(yù)定邏輯順序高效執(zhí)行,本文就來介紹一下Java 實(shí)現(xiàn)線程池任務(wù)編排的示例代碼,感興趣的可以了解一下2024-10-10
Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)
Hibernate想要實(shí)現(xiàn)雙向的關(guān)聯(lián)就必須在映射文件的兩端同時(shí)配置<one-to-one>,另外還要在主映射的一端采用foreign外鍵關(guān)聯(lián)屬性,下面我們就一起來看一下Java的Hibernate框架中的雙向主鍵關(guān)聯(lián)與雙向外鍵關(guān)聯(lián)方法:2016-06-06
解決mybatis批量更新出現(xiàn)SQL報(bào)錯問題
這篇文章主要介紹了mybatis批量更新出現(xiàn)SQL報(bào)錯,解決辦法也很簡單只需要在application.properties配置文中的數(shù)據(jù)源url后面添加一個(gè)參數(shù),需要的朋友可以參考下2022-02-02
淺談s:select 標(biāo)簽中l(wèi)ist存放map對象的使用
下面小編就為大家?guī)硪黄獪\談s:select 標(biāo)簽中l(wèi)ist存放map對象的使用。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
Java基于狀態(tài)模式實(shí)現(xiàn)的文檔編輯模式切換功能實(shí)例
這篇文章主要介紹了Java基于狀態(tài)模式實(shí)現(xiàn)的文檔編輯模式切換功能,結(jié)合實(shí)例形式詳細(xì)分析了狀態(tài)模式的概念、原理及java使用狀態(tài)模式實(shí)現(xiàn)文檔編輯模式切換操作相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2018-05-05
解析整合mybatis-spring需要的maven依賴配置問題
這篇文章主要介紹了整合mybatis-spring需要的maven依賴配置問題,創(chuàng)建Maven項(xiàng)目,導(dǎo)入相關(guān)jar包,文中還給大家提到了,解決maven靜態(tài)資源約定大于習(xí)慣問題,本文給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-11-11

