java.io.EOFException : SSL peer shut down incorrectly 异常

标签 java ssl

我有以下代码:

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

当我运行代码时,我收到以下异常消息: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

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

最佳答案

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

上面的Exception 是我们在客户端遇到的一个通用异常,如果有以下任何一种情况:

  • 如果服务器和客户端支持不同版本的 TLS,例如服务器 支持 TLS2 而客户端仅支持 TLS1。这个问题可以通过在客户端设置以下属性来解决:

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

  • 如果服务器无法验证客户端的证书链,那么它也会关闭连接。

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

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

关于java.io.EOFException : SSL peer shut down incorrectly 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30628037/

相关文章:

java - 如何在 Java Swing 中创建投影、内发光和外发光?

java - 在 Python 中创建 JSON 并在 Java 中解析

python - SSL 认证错误 > 主机名不匹配

google-chrome - 匿名代理不仅仅适用于 Firefox。对于 Chrome - 确定

java - 将编辑文本中的项目添加到微调器

java - Elasticsearch:获取路径下的嵌套对象不是嵌套类型

java - 项目 react 堆 : Flux Timeout only if first item is not emitted

apache - 虚假请求,证书审核员?

curl - Heroku SSL 问题给我验证失败

sockets - 如何在 JAVA JSSE 中操作 TLS/SSL 中的握手消息?