java - POST 请求的 I/O 错误... java.net.SocketException : Connection reset

标签 java spring spring-boot

我正在使用 Spring Rest Template调用托管在 SSL 端点上的 REST 服务。在第一个请求之后,我收到以下错误。环境是AWS EC2,Open JDK 1.8.161 , Linux .端点仅支持TLSv1.21.3 .

 org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://mycompany.com": Connection reset; nested exception is java.net.SocketException: Connection reset
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743)
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:686)
        at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:437)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:855)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
    ... more stack here
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:210)
    at java.net.SocketInputStream.read(SocketInputStream.java:141)
    at sun.security.ssl.InputRecord.readFully(InputRecord.java:465)
    at sun.security.ssl.InputRecord.read(InputRecord.java:503)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:983)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:940)
    at sun.security.ssl.AppInputStream.read(AppInputStream.java:105)
    at .....
    ... 107 common frames omitted

任何指导将不胜感激=!

最佳答案

这很像协议(protocol)问题。 AWS 完全支持 TLS 1.2 或更高版本,但客户端不支持。 Rest Template如果没有另外配置,将与 TLS 1.0 开始协商.您必须强制使客户端仅与 1.2 或 1.3 协议(protocol)栈连接。

通常,这可以在创建客户端时进行配置。类似的东西

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);

您可以找到如何根据 spring 版本调整模板。强制客户端仅在允许的协议(protocol)上进行协商将解决此问题。

关于java - POST 请求的 I/O 错误... java.net.SocketException : Connection reset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52836065/

相关文章:

java - hibernate 实体到json

java - 数据库未在实体类上使用注释创建

java - Selenium webdriver中的sessionID如何在并行执行期间为每个浏览器实例获取不同的值

Java 泛型工厂模式

spring - Controller层可以直接调用Persistence层吗?

java - 如何从 SpringBoot 应用程序发出自定义指标并在 PCF Autoscaler 中使用它

spring - 将EJB注入(inject)Spring Boot环境

java - 使用java api连接Hbase时卡住

java - 尝试渲染 pdf 时获取此 "org.xhtmlrenderer.render.BlockBox cannot be cast to org.xhtmlrenderer.newtable.TableBox"

java - Spring Cloud 2020.0 不再处理 bootstrap.yml 配置