2017-04-05 73 views
0

形勢

我有一個連接到測試具有自簽名證書服務器來測試應用程序。要做到這一點我做到以下幾點:RestTemplateBuilder定製CloseableHttpClient仍然拋出SSLPeerUnverifiedException

  1. 我使用RestTemplateBuilder從Spring框架創建RestTemplate
  2. 我通過自定義配置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(); 
    } 
} 

回答

0

你使用從春季(引導)合適的註釋?

至少如果它是Spring Boot應用程序,則必須使用@ConfigurationgetRestTemplate(...)以及@Bean註釋該類。

相關問題