中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

中山制作網(wǎng)站的公司西安網(wǎng)站推廣慧創(chuàng)科技

中山制作網(wǎng)站的公司,西安網(wǎng)站推廣慧創(chuàng)科技,網(wǎng)站建設(shè) 軟件開發(fā)的公司排名,淘寶上可以做網(wǎng)站嗎本文將展示如何配置Apache HttpClient 4和5以支持“接受所有”SSL。 目標(biāo)很簡單——訪問沒有有效證書的HTTPS URL。 SSLPeerUnverifiedException 在未配置SSL的情況下,嘗試消費一個HTTPS URL時會遇到以下測試失敗: Test void whenHttpsUrlIsConsumed…

本文將展示如何配置Apache HttpClient 4和5以支持“接受所有”SSL。

目標(biāo)很簡單——訪問沒有有效證書的HTTPS URL。

SSLPeerUnverifiedException

在未配置SSL的情況下,嘗試消費一個HTTPS URL時會遇到以下測試失敗:

@Test
void whenHttpsUrlIsConsumed_thenException() {String urlOverHttps = "https://localhost:8082/httpclient-simple";HttpGet getMethod = new HttpGet(urlOverHttps);assertThrows(SSLPeerUnverifiedException.class, () -> {CloseableHttpClient httpClient = HttpClients.createDefault();HttpResponse response = httpClient.execute(getMethod, new CustomHttpClientResponseHandler());assertThat(response.getCode(), equalTo(200));});
}

具體的失敗信息是:

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticatedat sun.security.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:397)at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:126)...

當(dāng)無法為URL建立有效的信任鏈時,就會拋出javax.net.ssl.SSLPeerUnverifiedException異常。

配置SSL - 接受所有(HttpClient 5)

現(xiàn)在讓我們配置HTTP客戶端以信任所有證書鏈,無論其有效性如何:

@Test
void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException, IOException {final HttpGet getMethod = new HttpGet(HOST_WITH_SSL);final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create().register("https", sslsf).register("http", new PlainConnectionSocketFactory()).build();final BasicHttpClientConnectionManager connectionManager =new BasicHttpClientConnectionManager(socketFactoryRegistry);try (CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();CloseableHttpResponse response = (CloseableHttpResponse) httpClient.execute(getMethod, new CustomHttpClientResponseHandler())) {final int statusCode = response.getCode();assertThat(statusCode, equalTo(HttpStatus.SC_OK));}
}

通過新的TrustStrategy覆蓋標(biāo)準(zhǔn)證書驗證過程后,測試現(xiàn)在可以通過,客戶端能夠成功消費HTTPS URL。

配置SSL - 接受所有(HttpClient 4.5)

對于HttpClient 4.5版本,配置方式類似,但使用了一些不同的API:

@Test
public final void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk()throws GeneralSecurityException {TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create().register("https", sslsf).register("http", new PlainConnectionSocketFactory()).build();BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry);CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).setConnectionManager(connectionManager).build();HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);assertThat(response.getStatusCode().value(), equalTo(200));
}

Spring RestTemplate與SSL(HttpClient 5)

了解了如何配置帶有SSL支持的基本HttpClient之后,我們來看看更高級別的客戶端——Spring RestTemplate。

在沒有配置SSL的情況下,預(yù)期的測試會失敗:

@Test
void whenHttpsUrlIsConsumed_thenException() {final String urlOverHttps = "https://localhost:8443/httpclient-simple/api/bars/1";assertThrows(ResourceAccessException.class, () -> {final ResponseEntity<String> response = new RestTemplate().exchange(urlOverHttps, HttpMethod.GET, null, String.class);assertThat(response.getStatusCode().value(), equalTo(200));});
}

接下來,配置SSL來解決這個問題:

@Test
void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException {final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true;final SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);final Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create().register("https", sslsf).register("http", new PlainConnectionSocketFactory()).build();final BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry);final CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connectionManager).build();final HttpComponentsClientHttpRequestFactory requestFactory =new HttpComponentsClientHttpRequestFactory(httpClient);final ResponseEntity<String> response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);assertThat(response.getStatusCode().value(), equalTo(200));
}

這里配置方式與直接使用HttpClient非常相似,我們用帶有SSL支持的請求工廠配置了RestTemplate。

結(jié)論

本教程討論了如何配置Apache HttpClient以使其能夠消費任何HTTPS URL,無論證書的有效性如何。

同樣也展示了如何對Spring RestTemplate進行同樣的配置。

重要的是要理解這種策略完全忽略了證書檢查——這使得它不安全,僅應(yīng)在合理的情況下使用。

http://www.risenshineclean.com/news/7984.html

相關(guān)文章:

  • 互聯(lián)網(wǎng)下的網(wǎng)絡(luò)營銷前端seo是什么意思
  • 網(wǎng)站建設(shè)營銷方案整站外包優(yōu)化公司
  • 泉州北京網(wǎng)站建設(shè)如何制作app軟件
  • phpcms學(xué)校網(wǎng)站模板如何制作微信小程序店鋪
  • wordpress 社交分享肇慶seo排名外包
  • 網(wǎng)站倒計時代碼資源企業(yè)網(wǎng)站排名優(yōu)化價格
  • html制作網(wǎng)站的步驟網(wǎng)絡(luò)服務(wù)包括
  • 企業(yè)域名是什么網(wǎng)站seo關(guān)鍵詞設(shè)置
  • 網(wǎng)站設(shè)計營銷網(wǎng)站出租三級域名費用
  • 做視頻網(wǎng)站視頻的軟件企業(yè)營銷培訓(xùn)課程
  • 女性時尚網(wǎng)站源碼客戶關(guān)系管理
  • 有沒有免費的微網(wǎng)站視頻營銷模式有哪些
  • 昭通網(wǎng)站建設(shè)如何提高網(wǎng)站排名的方法
  • 二手站網(wǎng)站怎做優(yōu)化課程體系
  • 招聘 負(fù)責(zé)網(wǎng)站開發(fā)網(wǎng)絡(luò)營銷有什么方式
  • 網(wǎng)站做cdn百度網(wǎng)頁版入口
  • 網(wǎng)站信息發(fā)布制度建設(shè)seo網(wǎng)站優(yōu)化排名
  • 建網(wǎng)站哪便宜百度網(wǎng)站提交入口網(wǎng)址
  • 網(wǎng)頁跟網(wǎng)站的區(qū)別百度seo2022
  • 開發(fā)app的注意事項seo代理計費系統(tǒng)
  • 兩耳清風(fēng)怎么做網(wǎng)站南京網(wǎng)絡(luò)優(yōu)化培訓(xùn)
  • 校園網(wǎng)站建設(shè)論文域名大全查詢
  • wordpress+4.9+google蘋果aso優(yōu)化
  • 做網(wǎng)站還要寫文章嗎品牌運營中心
  • 做圖片網(wǎng)站用什么程序百度地圖導(dǎo)航手機版免費下載
  • 可以先做網(wǎng)站后備案么app拉新推廣平臺有哪些
  • 2345網(wǎng)址導(dǎo)航手機上網(wǎng)導(dǎo)航下載seo網(wǎng)絡(luò)推廣教程
  • 秦皇島建設(shè)網(wǎng)站品牌宣傳方式
  • 網(wǎng)絡(luò)營銷方式的使用方法搜索引擎優(yōu)化有哪些要點
  • 長春關(guān)鍵詞推廣快速排名優(yōu)化seo