0
votes

I use the spring boot RestTemplate to make a json request to the public network IP, but it always displays disconnected and reconnected a minute later

Log as follows:

[] | [20181210 09:11:58.092] | [DEBUG] | [person-certify-7c46cfc9d-t4h7m] | [http-nio-8184-exec-10] | [org.apache.http.wire] | --> http-outgoing-15 << "end of stream"| [person-certify-7c46cfc9d-t4h7m] [person-certify]- | [] | [20181210 09:11:58.092] | [DEBUG] | [person-certify-7c46cfc9d-t4h7m] | [http-nio-8184-exec-10] | [o.a.h.i.c.DefaultManagedHttpClientConnection] | --> http-outgoing-15: Close connection| [person-certify-7c46cfc9d-t4h7m] [person-certify]- | [] | [20181210 09:12:58.092] | [DEBUG] | [person-certify-7c46cfc9d-t4h7m] | [http-nio-8184-exec-10] | [o.a.h.i.c.PoolingHttpClientConnectionManager] | --> Connection leased: [id: 17][route: {}->http://118.25.31.127:80][total kept alive: 1; route allocated: 1 of 1000; total allocated: 2 of 1000]|

1

1 Answers

0
votes

Looks like your target servers only accept TLSv1.2 connections you must make sure that your code is only performing connections with that protocol which it seems your code is not doing. Can you please check your logs ( debug mode ) and see what is version? What spring version you are using?

Something like should show in logs during handshake in debug mode...

.....SSLConnectionSocketFactory - Enabled protocols: [TLSv1]

How to configure TLSv2 is already answered in this thread How to enforce TLS1.2 to Rest client using Rest Template

in Short you can try

import javax.net.ssl.SSLContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

SSLContext context = SSLContext.getInstance("TLSv1.2");
context.init(null, null, null);

CloseableHttpClient httpClient = HttpClientBuilder.create().setSSLContext(context)
    .build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
RestTemplate restTemplate = new RestTemplate(factory);