web项目不免会用上Http请求有些地址没有安全证书,Java默认是不能访问这些地址的,通常的方法是忽略安全证书,网上查了一圈,看似不少资料,但我却没找到一个拿来就能够用的,最后整合了网上的信息,弄出来一个可用的,我也不肯定我这个是否是通用的, 毕竟其实都还不太清楚原理。只是记录一下:web
//忽略安全证书认证的链接方式 private static RestTemplate ignoreSSL() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true; SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom() .loadTrustMaterial(null, new TrustSelfSignedStrategy()) .build(); SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); CloseableHttpClient httpClient = HttpClients.custom() .setSSLSocketFactory(csf) .build(); HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); requestFactory.setHttpClient(httpClient); return new RestTemplate(requestFactory); }