javascript
在Spring Rest模板中跳过SSL证书验证
使用Spring Rest模板時如何跳過SSL證書驗證? 配置Rest Template,以便它使用Http Client創(chuàng)建請求。
注意:如果您熟悉sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target則下面的內(nèi)容應(yīng)該sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target您有所幫助。
Http客戶端
首先,將HttpClient (> 4.4)導入到您的項目中
compile('org.apache.httpcomponents:httpclient:4.5.1')配置RestTemplate
使用Http客戶端的SSLContexts工廠方法配置SSLContext :
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();HttpComponentsClientHttpRequestFactory requestFactory =new HttpComponentsClientHttpRequestFactory();requestFactory.setHttpClient(httpClient);RestTemplate restTemplate = new RestTemplate(requestFactory);org.apache.http.ssl.TrustStrategy用于覆蓋標準證書驗證過程。 在上面的示例中-它始終返回true ,因此無需進一步驗證即可信任證書。
考試
@Test public void opensSSLPage() throws Exception {String uri = "https://some-secured-page.com";ResponseEntity<String> entity = restTemplate.getForEntity(uri, String.class);assertThat(entity.getStatusCode().is2xxSuccessful()).isTrue(); }最后的話
上面的代碼在某些情況下會有所幫助(例如,對具有自簽名證書的服務(wù)器進行測試),但是不應(yīng)在生產(chǎn)中使用它-除非您100%確信自己在做什么。
翻譯自: https://www.javacodegeeks.com/2016/02/skip-ssl-certificate-verification-spring-rest-template.html
總結(jié)
以上是生活随笔為你收集整理的在Spring Rest模板中跳过SSL证书验证的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 常见的加密方式
- 下一篇: glassfish5_将Glassfis
