Jetty Websocket连接 - 忽略自签名证书

时间:2014-12-05 21:27:57

标签: java ssl websocket

是否有办法(打开浏览器并接受自签名证书除外)告诉Jetty WebSockets在打开websocket连接时忽略自签名证书错误?我已经验证了我的代码在存在有效证书的环境中工作,因此这肯定与在其他环境中使用自签名证书有关。

 public static void main(String[] args) {
    String destUri = "wss://example.ws.uri.com";
    if (args.length > 0) {
        destUri = args[0];
    }


    SslContextFactory sslContextFactory = new SslContextFactory();
    WebSocketClient client = new WebSocketClient(sslContextFactory);
    SimpleEchoSocket socket = new SimpleEchoSocket();
    try {
        client.start();
        URI echoUri = new URI(destUri);
        ClientUpgradeRequest request = new ClientUpgradeRequest();
        client.connect(socket, echoUri, request);
        System.out.printf("Connecting to : %s%n", echoUri);
        socket.awaitClose(5, TimeUnit.SECONDS);
    } catch (Throwable t) {
        t.printStackTrace();
    } finally {
        try {
            client.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

你可以告诉SslContextFactory不验证证书

SslContextFactory sec = new SslContextFactory();
sec.setValidateCerts(false);