我正在使用Spring RestTemplate发出HTTPS请求,并且我想忽略SSL证书
这是我创建restTemplate请求的代码:
TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String
authType) -> true;
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
.loadTrustMaterial(null, acceptingTrustStrategy)
.build();
SSLConnectionSocketFactory csf = new
SSLConnectionSocketFactory(sslContext);
loseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(csf)
.build();
HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
RestTemplate restTemplate = new RestTemplate(requestFactory);
...
response = restTemplate.exchange("https://192.168.1.2:/foo/bar",
HttpMethod.POST, entity,String.class);
此请求在我使用服务器主机名时有效,但是使用服务器IP地址时出现以下异常:
Exception in thread "main"
org.springframework.web.client.ResourceAccessException: I/O error on POST
request for "https://192.168.1.2/foo/bar": Certificate for
<192.168.1.2> doesn't match any of the subject alternative names: [];
nested exception is javax.net.ssl.SSLPeerUnverifiedException: Certificate
for <192.168.1.2> doesn't match any of the subject alternative names: []
Caused by: javax.net.ssl.SSLPeerUnverifiedException: Certificate for
<192.168.1.2> doesn't match any of the subject alternative names: []