0
形勢
我有一個連接到測試具有自簽名證書服務器來測試應用程序。要做到這一點我做到以下幾點:RestTemplateBuilder定製CloseableHttpClient仍然拋出SSLPeerUnverifiedException
- 我使用RestTemplateBuilder從Spring框架創建RestTemplate
- 我通過自定義配置HttpClient的應該忽略主機名驗證。
問題
儘管自定義HTTP客戶端的我仍然得到SSLPeerUnverifiedException。任何線索?
代碼
負責配置RestTemplate部分代碼:
public class RestTemplateForBasicAuth {
private RestTemplateBuilder restTemplateBuilder;
public RestTemplateForBasicAuth(RestTemplateBuilder restTemplateBuilder) {
this.restTemplateBuilder = restTemplateBuilder;
}
public RestTemplate getRestTemplate(String host) {
// Initialize http client that does not complain on self signed certificate
CloseableHttpClient httpClient = HttpClients.custom().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
HttpHost httpHost = new HttpHost(host);
final AuthHttpComponentsClientHttpRequestFactory requestFactory = new AuthHttpComponentsClientHttpRequestFactory(httpClient, httpHost, "user", "xxx");
restTemplateBuilder.requestFactory(requestFactory);
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
restTemplateBuilder.messageConverters(messageConverter);
return restTemplateBuilder.build();
}
}