1.簡介Https
HTTPS協議 = HTTP協議 + SSL/TLS協議,在HTTPS數據傳輸的過程中,需要用SSL/TLS對數據進行加密和解密,需要用HTTP對加密后的數據進行傳輸,由此可以看出HTTPS是由HTTP和SSL/TLS一起合作完成的。
一個HTTPS證書的價格還是有點貴,國內有一些雲服務器廠商提供免費的HTTPS證書。不過在JDK中提供了一個Java數字證書管理工具keytool,在\jdk\bin目錄下,通過這個工具可以自己生成一個數字證書,命令如下
keytool -genkey -alias tomcathttps -keyalg RSA -keysize 2048 -keystore sang.p12 -validity 365
2. 在Spring boot中利用HttpClientUtil 實現http/https請求
試過Spring的RestTemplate,好像Apache的Http Client更合適
2.1 導入jar包
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.7</version> </dependency>
2.2 編寫HttpClientUtil
import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.TrustStrategy; import org.apache.http.util.EntityUtils; import javax.net.ssl.SSLContext; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class HttpUtils { //Http協議GET請求 public static String httpGet(String url) throws Exception{ //初始化HttpClient CloseableHttpClient httpClient = HttpClients.createDefault(); //創建HttpGet HttpGet httpGet = new HttpGet(url); //發起請求,獲取response對象 CloseableHttpResponse response = httpClient.execute(httpGet); //獲取請求狀態碼 //response.getStatusLine().getStatusCode(); //獲取返回數據實體對象 HttpEntity entity = response.getEntity(); //轉為字符串 String result = EntityUtils.toString(entity,"UTF-8"); return result; } //Http協議Post請求 public static String httpPost (String url,String json) throws Exception{ //初始HttpClient CloseableHttpClient httpClient = HttpClients.createDefault(); //創建Post對象 HttpPost httpPost = new HttpPost(url); //設置Content-Type httpPost.setHeader("Content-Type","application/json"); //寫入JSON數據 httpPost.setEntity(new StringEntity(json)); //發起請求,獲取response對象 CloseableHttpResponse response = httpClient.execute(httpPost); //獲取請求碼 //response.getStatusLine().getStatusCode(); //獲取返回數據實體對象 HttpEntity entity = response.getEntity(); //轉為字符串 String result = EntityUtils.toString(entity,"UTF-8"); return result; } //Https協議Get請求 public static String httpsGet(String url) throws Exception{ CloseableHttpClient hp = createSSLClientDefault(); HttpGet hg = new HttpGet(url); CloseableHttpResponse response = hp.execute(hg); HttpEntity entity = response.getEntity(); String content = EntityUtils.toString(entity,"UTF-8"); hp.close(); return content; } //Https協議Post請求 public static String httpsPost(String url, String json) throws Exception{ CloseableHttpClient hp = createSSLClientDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type","application/json"); httpPost.setEntity(new StringEntity(json)); CloseableHttpResponse response = hp.execute(httpPost); HttpEntity entity = response.getEntity(); String content = EntityUtils.toString(entity,"UTF-8"); hp.close(); return content; } public static CloseableHttpClient createSSLClientDefault() throws Exception{ //如果下面的方法證書還是不過,報錯的話試試下面第二種 /* SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy(){ //信任所有 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); return HttpClients.custom().setSSLSocketFactory(sslsf).build();*/ SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } }
2.3 編寫測試類
@Test public void contextLoads() { try { String result = HttpUtils.httpGet("https://XXX"); System.out.println("返回值>>>:"+result); } catch (Exception e) { e.printStackTrace(); } }
這樣就可以成功發送HTTPS請求了!
如果想嘗試Spring的RestTemplate,可以參考:https://www.cnblogs.com/softidea/p/10663849.html
補充
如果報如下錯誤,只需要在JDK中添加安全證書就好:
sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
添加證書命令如下:
- 在命令行切換到證書解壓后所在目錄 cd /d D:\cert
- 設置JDK的路徑 set cacerts="C:\Program Files\java\jdk1.8.0_231\jre\lib\security\cacerts"
- 添加證書 keytool -importcert -keystore %cacerts% -alias nexus -file D:\cert\nexus.cer -storepass changeit -noprompt
- 查看證書列表 keytool -list -v -alias cacert -keystore "C:/Program Files/java/jdk1.8.0_231/jre/lib/security/cacerts" -storepass changeit