【问题标题】:SSL peer shut down incorrectly in JavaJava 中的 SSL 对等点错误关闭
【发布时间】:2015-05-08 15:37:46
【问题描述】:

我需要通过 HTTPS 协议发出请求。我写了以下代码:

import java.net.HttpURLConnection;
import java.net.URL;

import org.junit.Test;

public class XMLHandlerTest {
    private static final String URL = "https://ancine.band.com.br/xml/pgrt1_dta_20150303.xml";

    @Test
    public void testRetrieveSchedule() {
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(URL).openConnection();
            connection.setRequestMethod("HEAD");
            int responseCode = connection.getResponseCode();
            System.out.println(responseCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

我得到了带有 java.io.EOFException 的异常堆栈跟踪:

javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:953)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1332)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1359)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1343)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1301)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:468)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:338)
    at br.com.onebr.onesocial.arte1.service.core.scheduler.Arte1XMLHandlerTest.testRetrieveSchedule(Arte1XMLHandlerTest.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
    at sun.security.ssl.InputRecord.read(InputRecord.java:482)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:934)
    ... 32 more

我收到了来自 https://google.com 的成功回复,但上面的 URL (https://ancine.band.com.br/xml/pgrt1_dta_20150303.xml) 出现了这个错误。

使用 PHP、.NET 和 NodeJS,该 URL 工作正常。

有人知道为什么会这样吗?

【问题讨论】:

  • 你使用哪个java版本来运行程序?
  • 请使用-Djavax.net.debug=ssl,handshake 运行您的客户端并在您的问题中发布输出。
  • 我正在使用 OpenJDK 运行时环境 (IcedTea 2.5.4) (7u75-2.5.4-1~trusty1).

标签: java ssl


【解决方案1】:

这是安全协议的问题。我正在使用 TLSv1,但主机只接受 TLSv1.1 和 TLSv1.2,然后我使用以下指令更改了 Java 中的协议:

System.setProperty("https.protocols", "TLSv1.1");

【讨论】:

  • 如何得知主机接受了哪个版本的安全协议?
  • @MasterJoe2 你可以使用 nmap nmap --script ssl-enum-ciphers -p 443 {HOSTNAME}
【解决方案2】:

您可以在系统属性中将协议版本设置为:

克服 ssl 握手错误

System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");

【讨论】:

    【解决方案3】:

    除了接受的答案外,其他问题也可能导致异常。对我来说,证书不受信任(即自签名证书,不在信任库中)。

    如果证书文件不存在,或者无法加载(例如路径中的拼写错误),在某些情况下可能会导致相同的异常。

    【讨论】:

    • 如果是自签名证书,如何解决这个错误?
    • 将其添加到您的信任库(添加到操作系统范围的信任库,或者在使用-Djavax.net.ssl.trustStore=...-Djavax.net.ssl.trustStorePassword=... 启动JVM 时定义一个。)
    • @mani_nz,这可能会给你一个想法:youtu.be/l4_JIIrMhIQ
    • 你 100% 确定这是同一个错误吗?我很难相信自签名证书会导致“SSL 对等体错误关闭”错误。
    • @eis 我也很惊讶。但这发生在几年前,仍然是 Java SE 8。从那时起,TLS 处理发生了很多变化,所以我的回答可能不再有效。
    【解决方案4】:

    请关闭android studio并删除文件

    .gradle
    

    .idea
    

    从您的项目中归档。希望对您有所帮助

    位置:转到 Android Studio 项目->您的项目->查看两个文件删除 (.gradle & .idea)

    【讨论】:

      【解决方案5】:

      此错误是安全库的通用错误,可能在其他情况下发生。 以防其他人在将带有 javax.mail 的电子邮件发送到 smtp 服务器时遇到同样的错误。然后强制其他协议的代码设置如下属性:

      prop.put("mail.smtp.ssl.protocols", "TLSv1.2");            
      
      //And just in case probably you need to set these too
      prop.put("mail.smtp.starttls.enable", true);    
      prop.put("mail.smtp.ssl.trust", {YOURSERVERNAME});
      

      【讨论】:

        【解决方案6】:

        以下代码对我有用 您必须在surefire插件中添加以下配置

        <configuration> <argLine>-Dhttps.protocols=TLSv1.1</argLine> </configuration>

        【讨论】:

          【解决方案7】:

          我遇到了同样的问题,对我来说,将证书添加到信任存储解决了这个问题。

          【讨论】:

            【解决方案8】:

            我有一个类似的问题,通过取消选中 java 高级安全中的选项“使用 SSL 2.0 兼容的 ClientHello 格式。

            【讨论】:

              【解决方案9】:

              接受的答案在我的情况下不起作用,不知道为什么。我从 JRE1.7 切换到 JRE1.8 并自动解决了问题。 JRE1.8默认使用TLS1.2

              【讨论】:

                【解决方案10】:

                我在 java 8 上使用 SSL/TLS 服务器套接字库时遇到了这个异常。将 jdk 更新到 14(以及 VM 到 14)解决了这个问题。

                【讨论】:

                  【解决方案11】:

                  我遇到了同样的问题,就像我想的其他人一样。添加 System.setProperties(....) 并没有为我解决这个问题。

                  所以我的电子邮件客户端是在一个单独的项目中上传到一个工件。我将此项目作为 gradle 依赖项导入到其他项目中。我的问题是我在 build.gradle 中为 javax.mail 使用了 implementation,这导致下游出现问题。
                  我将此行从implementation 更改为api,我的下游项目开始工作并再次连接。

                  【讨论】:

                    【解决方案12】:

                    我在我的 Spring Boot 应用程序上启用了双向 SSL,我的 Jenkins 管道正在重新构建 docker,启动组合,然后运行集成测试,但每次都因此错误而失败。 每次在同一台 Jenkins 机器上的独立测试中,我都能够测试正在运行的 docker,而不会出现这个 SSL 错误。 事实证明,当测试开始执行时,服务器并没有完全启动。 在我的 bash 脚本中休眠几秒钟以允许 Spring boot 应用程序启动并运行完全解决了这个问题。

                    【讨论】:

                      【解决方案13】:

                      在我的例子中,我正在向使用 SSLV3 的系统发出请求。 所以我添加了以下 jvm 选项。问题就解决了。

                      -Dhttps.protocols="TLSv1,TLSv1.1,TLSv1.2"
                      

                      【讨论】:

                        【解决方案14】:

                        有时可能是网络问题造成的。 不知道是什么原因,切换到其他网络后,应用编译安装成功。

                        【讨论】:

                          【解决方案15】:

                          对我来说,设置-Dhttps.protocols="TLSv1,TLSv1.1,TLSv1.2"(或-Dhttps.protocols="TLSv1.2")不起作用。

                          由于我在 AWS K8S 集群的 Docker 容器中运行有问题的代码,因此我在 [AWS 文档][1] 中找到了类似的解决方案 设置此系统属性-Djdk.tls.client.protocols=TLSv1.2后,错误消失了。

                          [更新] 将其部署到生产环境后,我注意到这种行为是断断续续的:有时我的电子邮件被发送了,有时他们没有。 在进行更多研究时,我发现我的客户端使用的是服务器不支持的 TLS 1.0(它只支持 TLS 1.2) 我收到的详细错误消息是这样的:

                          javax.net.ssl|SEVERE|10|grpc-default-executor-0|2021-10-22 19:38:48.017 GMT|Logger.java:765|Fatal (HANDSHAKE_FAILURE): Couldn't kickstart handshaking

                          最后,经过多次试验,我用这里的答案解决了这个问题:How to force JavaMailSenderImpl to use TLS1.2? [1]:https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/security-java-tls.html

                          【讨论】:

                            【解决方案16】:

                            正如@Zigri2612 所建议的,对我有用的是添加 TLS https.protocols,但我必须使用空格而不是逗号分隔值:

                            System.setProperty("https.protocols", "TLSv1 TLSv1.1 TLSv1.2 TLSv1.3");
                            

                            【讨论】:

                              【解决方案17】:

                              我也遇到过同样的错误信息,但原因和解决方法很有趣:

                              我的问题通过将 openjdk 从 8u40 升级到 8u191 或更高版本 8u312 解决了,

                              根本原因是一种互操作性问题,似乎客户端和服务器使用不同的填充方法或密码参数(例如 EC 曲线)进行 DH 密钥交换,

                              所以如果你用完了解决方案,可能你可以尝试升级你的 jdk(不要只是升级到主要版本,先尝试升级到更高的次要版本,请注意我的问题已由更高次要解决openjdk8uXXX 的版本,但 openjdk9 仍然失败

                              您可以参考(握手部分)的整个故障排除过程: https://lyhistory.github.io/docs/software/network/http_ssl_tls_setup.html

                              【讨论】:

                                【解决方案18】:

                                检查您的互联网连接,然后重新同步您的项目。

                                【讨论】:

                                  猜你喜欢
                                  • 2021-12-04
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 1970-01-01
                                  • 2018-05-17
                                  • 2013-05-19
                                  • 1970-01-01
                                  • 2014-02-06
                                  • 1970-01-01
                                  相关资源
                                  最近更新 更多