我必须连接到具有自签名证书的测试服务器来测试应用程序。为此,我执行以下操作:
尽管有自定义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 :(得分:0)
你是否使用了Spring(Boot)中正确的Annotations?
至少如果它是Spring Boot应用程序,您必须使用@Configuration
和方法getRestTemplate(...)
注释@Bean
。