RestTemplate忽略https证书认证
程序员文章站
2022-05-31 16:10:02
...
public static RestTemplate buildRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
if (ignoreSSL) {//ignoreSSL为true时,绕过证书
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectionRequestTimeout(300000);
factory.setConnectTimeout(300000);
factory.setReadTimeout(300000);
// https
CloseableHttpClient httpClient = getHttpsClient();
factory.setHttpClient(httpClient);
restTemplate = new RestTemplate(factory);
}
reInitMessageConverter(restTemplate);
return restTemplate;
}
public static CloseableHttpClient getHttpsClient() { CloseableHttpClient httpClient; if (ignoreSSL) {//ignoreSSL为true时,绕过证书 SSLContext sslContext = null; try { sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { return true; } }).build(); } catch (NoSuchAlgorithmException e) { e.getStackTrace(); } catch (KeyManagementException e) { e.getStackTrace(); } catch (KeyStoreException e) { e.getStackTrace(); } httpClient = HttpClients.custom().setSSLContext(sslContext). setSSLHostnameVerifier(new NoopHostnameVerifier()).build(); } else { httpClient = HttpClients.createDefault(); } return httpClient; }
推荐阅读
-
.net core signalR 忽略https 证书
-
PHP Curl https跳过ssl证书认证报错记录及解决
-
【SSL证书行业】HTTPS认证支撑网络安全的防护门!
-
httpclient 忽略https,信任所有证书
-
HTTPS认证二: openssl生成证书及签名
-
使用RestTemplate调用https接口跳过证书验证
-
RestTemplate添加HTTPS证书全过程解析
-
探究公钥、私钥、对称加密、非对称加密、hash加密、数字签名、数字证书、CA认证、https它们究竟是什么,它们分别解决了通信过程的哪些问题。
-
httpclient访问需要客户端认证数字证书的https接口的处理方法
-
Spring5 WebClient忽略https证书认证