Игнорировать проверку сертификата SSL при использовании Spring RestTemplate - PullRequest
0 голосов
/ 04 января 2019

Я использую 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: []
...