为了与项目的其他部分保持一致,我需要使用restempate,并且我使用bufferingclienthttpprequestfactory,因为响应被截获并注销了主体。
我所拥有的
在springbootapplication类中
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
RestTemplate template = builder
.setReadTimeout((int) Duration.ofSeconds(10).toMillis())
.setConnectTimeout((int)Duration.ofSeconds(10).toMillis())
.build();
template.setRequestFactory(new BufferingClientHttpRequestFactory(factory));
List<ClientHttpRequestInterceptor> interceptorList = new ArrayList<ClientHttpRequestInterceptor>();
interceptorList.add(new LoggingInterceptor());
template.setInterceptors(interceptorList);
return template;}
在役
private ResponseEntity exchangeWithEndpointInternal(RequestEntity<?> request) throws IOException {
Instant requestStart = this.getClock().instant();
ResponseEntity response;
response = restTemplate.exchange(request, String.class);
Instant requestEnd = this.getClock().instant();
return response;
}
我尝试的是:
重写bean定义中的verify方法
template.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory() {
@Override
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
if (connection instanceof HttpsURLConnection) {
((HttpsURLConnection) connection).setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
}
super.prepareConnection(connection, httpMethod);
}
}));
使用noophostverifier
template.setRequestFactory(new BufferingClientHttpRequestFactory( new SimpleClientHttpRequestFactory() {
@Override
protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
if (connection instanceof HttpsURLConnection) {
((HttpsURLConnection) connection).setHostnameVerifier(new NoopHostnameVerifier());
}
super.prepareConnection(connection, httpMethod);
}
}));
两个都没有给我我想要的输出,我需要忽略ssl验证,但是我有一个错误:
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://someHost/somePath": PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:742)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:635)
at com.....exchangeWithEndpointInternal(FreightApiClientViaHttp.java:97)
at com.....exchangeWithEndpoint(FreightApiClientViaHttp.java:77)
... 73 common frames omitted
有什么想法吗?我很高兴更改工厂,如果我仍然可以读取响应体的多次示例,这是以前使用okhttpclient时使用的,没有resttemplate,类似这样:
final TrustManager[] trustAllCerts = new TrustManager[] {
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
// Install the all-trusting trust manager
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
// Create an ssl socket factory with our all-trusting manager
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
okHttpClientBuilder.sslSocketFactory(sslSocketFactory, (X509TrustManager)trustAllCerts[0]);
okHttpClientBuilder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
boolean whitelistedDomain = configuration.getSslDomainWhitelist().stream()
.peek(String::trim)
.filter(domain -> domain.equalsIgnoreCase(hostname))
.findAny()
.isPresent();
if (!whitelistedDomain) {
return OkHostnameVerifier.INSTANCE.verify(hostname, session);
} else {
return true;
}
}
});
但我不能用其他技术复制行为。
最后一点注意:当我用curl启动请求时,我得到一个ssl错误,当我添加-k标志时,一切正常。
暂无答案!
目前还没有任何答案,快来回答吧!