信任 Websocket SSL - 收到致命警报:handshake_failure

Posted

技术标签:

【中文标题】信任 Websocket SSL - 收到致命警报:handshake_failure【英文标题】:Trust Websocket SSL - Getting fatal alert: handshake_failure 【发布时间】:2016-04-14 20:54:23 【问题描述】:

我一直在研究为什么会得到:

java.util.concurrent.ExecutionException: javax.net.ssl.SSLException: Received fatal alert: handshake_failure

但我不知所措。这是我的代码:

import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.client.WebSocketClient;

import java.net.URI;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Future;

@WebSocket
public class ConnectDamnit 

    private static final Logger LOG = Log.getLogger(ConnectDamnit.class);

    public static void main(String[] args) throws NoSuchAlgorithmException
    
        String url = "wss://host.com:10443/listen";

        SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.setTrustAll(true); // The magic

        WebSocketClient client = new WebSocketClient(sslContextFactory);
        try
        
            client.start();
            ConnectDamnit socket = new ConnectDamnit();
            Future<Session> fut = client.connect(socket,URI.create(url));
            Session session = fut.get();
            session.getRemote().sendString("JSONDATA");
        
        catch (Throwable t)
        
            LOG.warn(t);
            t.printStackTrace(System.err);
        
    

    @OnWebSocketConnect
    public void onConnect(Session sess)
    
        LOG.info("onConnect()",sess);
    

    @OnWebSocketClose
    public void onClose(int statusCode, String reason)
    
        LOG.info("onClose(, )", statusCode, reason);
    

    @OnWebSocketError
    public void onError(Throwable cause)
    
        LOG.warn(cause);
    

    @OnWebSocketMessage
    public void onMessage(String msg)
    
        LOG.info("onMessage() - ", msg);
    

这是完整的错误:

java.util.concurrent.ExecutionException: javax.net.ssl.SSLException: Received fatal alert: handshake_failure
    at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:123)
    at JMeter.plugins.functional.samplers.websocket.ConnectDamnit.main(ConnectDamnit.java:40)
    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:497)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: javax.net.ssl.SSLException: Received fatal alert: handshake_failure
    at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
    at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1666)
    at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1634)
    at sun.security.ssl.SSLEngineImpl.recvAlert(SSLEngineImpl.java:1800)
    at sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:1083)
    at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:907)
    at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:781)
    at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624)
    at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.fill(SslConnection.java:502)
    at org.eclipse.jetty.io.ssl.SslConnection$DecryptedEndPoint.flush(SslConnection.java:802)
    at org.eclipse.jetty.io.WriteFlusher.completeWrite(WriteFlusher.java:400)
    at org.eclipse.jetty.io.ssl.SslConnection$1.run(SslConnection.java:97)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:607)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:536)
    at java.lang.Thread.run(Thread.java:745)

我可以使用 chrome 插件连接到这个套接字,所以我不知道为什么我不能在这里。任何指示或建议?我可以连接到 wss://echo.websocket.org/ 就好了

【问题讨论】:

您是否在服务器上使用自签名证书? 【参考方案1】:

更新了码头,不再报错..

<dependency>
  <groupId>org.eclipse.jetty.websocket</groupId>
  <artifactId>websocket-client</artifactId>
  <version>9.3.9.M1</version>
  <classifier>hybrid</classifier>
</dependency>

【讨论】:

以上是关于信任 Websocket SSL - 收到致命警报:handshake_failure的主要内容,如果未能解决你的问题,请参考以下文章

SSL:收到致命警报:handshake_failure

javax.net.ssl.SSLException:收到致命警报:带有 Selenium 的协议版本

ChangeCipherSpec - javax.net.ssl.SSLHandshakeException:收到致命警报:handshake_failure

SSL 握手错误 javax.net.ssl.SSLHandshakeException 收到致命警报 bad_certificate

javax.net.ssl.SSLException:收到致命警报:protocol_version

javax.net.ssl.SSLHandshakeException:收到致命警报:handshake_failure