网上查了一大堆 基本都是 keytool导入证书
keytool -import -alias ee -keystore "C:Program FilesJavajre1.8.0_221libsecuritycacerts" -file D:certee.cer -storepass changeit
但是:导入完之后我遇到了一个问题 莫名其妙的有时候好使有时候不好使
最终尝试了N+1次之后,发现 证书名字和别名是一个字母也提示添加成功 但是就是不好使,2个字母就好使(我也不知道什么原因)
RestTemplate POST 请求证书验证解决办法
final static RestTemplate restTemplate = new RestTemplate(generateHttpsRequestFactory());
private static HttpComponentsClientHttpRequestFactory generateHttpsRequestFactory() {
try {
TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory connectionSocketFactory =
new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient);
factory.setConnectTimeout(5 * 1000);
factory.setReadTimeout(5 * 1000);
return factory;
} catch (Exception e) {
throw new RuntimeException("创建HttpsRestTemplate失败", e);
}
}