java.io.EOFException:SSL 对等方关闭不正确

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30628037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:24:41  来源:igfitidea点击:

java.io.EOFException: SSL peer shut down incorrectly

javassl

提问by Sachin Kumar

I have the following code:

我有以下代码:

package daoImp;

import java.util.List;

import javapns.Push;
import javapns.communication.exceptions.KeystoreException;
import javapns.notification.PushedNotification;
import javapns.notification.ResponsePacket;
import org.json.JSONException;
import com.sun.jmx.snmp.daemon.CommunicationException;

public class Notification {

    public static void main(String args[]) {
        try {
            new Notification().sendMessageToAPN();
        } catch (CommunicationException | KeystoreException | JSONException
                | javapns.communication.exceptions.CommunicationException e) {
            e.printStackTrace();
        }
    }

    public void sendMessageToAPN() throws CommunicationException,
            KeystoreException, JSONException,
            javapns.communication.exceptions.CommunicationException {
        String regId1 = "6f9d340ab4d0f81206f7d8c1ab7b8994d90d139e0d1d2b99999b02887e60d54f";
        List<PushedNotification> notifications = Push.alert("hello","C:/Program Files (x86)/Java/jdk1.7.0_21/jre/lib/security/gameover.p12", "gameover",
                false, regId1);
        for (PushedNotification notification : notifications) {
            if (notification.isSuccessful()) {
                System.out.println("Push notification sent successfully to: " + notification.getDevice().getToken());

            } else {
                String invalidToken = notification.getDevice().getToken();
                System.err.println("Invalid Token " + invalidToken);

                System.out.println(" The problem was");
                Exception theProblem = notification.getException();
                theProblem.printStackTrace();

                ResponsePacket theErrorResponse = notification.getResponse();
                if (theErrorResponse != null) {
                    System.out.println(theErrorResponse.getMessage());
                }
            }
        }
    }
}

When I run the code, I get the following exception message: handshake to ssl failed as connection to remote host failed during handshake.

当我运行代码时,我收到以下异常消息:handshake to ssl failed as connection to remote host failed during handshake.

log4j:WARN No appenders could be found for logger (javapns.notification.Payload).
log4j:WARN Please initialize the log4j system properly.
Invalid Token 6f9d340ab4d0f81206f7d8c1ab7b6774d90d139e0d1d2b58599b02887e60d54f
 The problem was
javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
    at sun.security.ssl.AppOutputStream.write(Unknown Source)
    at java.io.OutputStream.write(Unknown Source)
    at javapns.notification.PushNotificationManager.sendNotification(PushNotificationManager.java:402)
    at javapns.notification.PushNotificationManager.sendNotification(PushNotificationManager.java:350)
    at javapns.notification.PushNotificationManager.sendNotification(PushNotificationManager.java:320)
    at javapns.Push.sendPayload(Push.java:177)
    at javapns.Push.alert(Push.java:47)
    at daoImp.Notification.sendMessageToAPN(Notification.java:27)
    at daoImp.Notification.main(Notification.java:16)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
    at sun.security.ssl.InputRecord.read(Unknown Source)
    ... 12 more

I don't know why I'm getting this message.

我不知道为什么我会收到这条消息。

回答by sauumum

javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
Caused by: java.io.EOFException: SSL peer shut down incorrectly

Above Exceptionis a generic exception we get in the client if there is any of below scenario:

Exception如果有以下任何一种情况,上面是我们在客户端中得到的一般异常:

  • If Server and Client support different version of TLS e.g. server support TLS2 while Client support only TLS1. This issue can be resolved by setting below property in Client side :
  • 如果服务器和客户端支持不同版本的 TLS,例如服务器支持 TLS2 而客户端仅支持 TLS1。这个问题可以通过在客户端设置以下属性来解决:

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

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

  • If the server is not able to validate the certificate chain of the client, then also it will close the conection.
  • 如果服务器无法验证客户端的证书链,那么它也会关闭连接。

Certificate chain qtp510727907-31, fatal error: 42: null cert chain javax.net.ssl.SSLHandshakeException: null cert chain %% Invalidated: [Session-3, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA]
qtp510727907-31, SEND TLSv1 ALERT: fatal, description = bad_certificate qtp510727907-31, WRITE: TLSv1 Alert, length = 2
main, WRITE: TLSv1 Change Cipher Spec, length = 1 qtp510727907-31, fatal: engine already closed. Rethrowing
javax.net.ssl.SSLHandshakeException: null cert chain

证书链 qtp510727907-31,致命错误:42:空证书链 javax.net.ssl.SSLHandshakeException:空证书链 %% 无效:[Session-3, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA]
qtp510727907_CBC_SHA ] qtp5107117907 - CBC_SHA ] qtp5107217907 - CBC_SHA ] qtp5107117907 - tlvts 5510727907 错误描述-31,写入:TLSv1 警报,长度 = 2
主要,写入:TLSv1 更改密码规范,长度 = 1 qtp510727907-31,致命:引擎已关闭。重新抛出
javax.net.ssl.SSLHandshakeException:空证书链

To know the exact cause of failure, we need to enable -Djavax.net.debug=allwhile executing the client call towards the server.

要知道失败的确切原因,我们需要在-Djavax.net.debug=all对服务器执行客户端调用时启用。

回答by untitledprogrammer

I believe you are missing your certificates.

我相信您缺少证书。

You can generate them using the InstallCerts app. (http://miteff.com/install-cert)

您可以使用 InstallCerts 应用程序生成它们。( http://miteff.com/install-cert)

or http://opentox.ntua.gr/blog/77-ssl-certificates

http://opentox.ntua.gr/blog/77-ssl-certificates

Once you get your certificate, you need to put it under your security directory within your jdk home, for example:

拿到证书后,需要将其放在jdk home下的security目录下,例如:

C:\Program Files\Java\jdk1.6.0_45\jre\lib\security

C:\Program Files\Java\jdk1.6.0_45\jre\lib\security

Hope this resolves your issue

希望这能解决您的问题