在ssl上的Jetty WebSocketClient

时间:2013-01-15 09:41:19

标签: java ssl websocket jetty

我有一个在嵌入式Jetty(8.1.8.v20121106)中运行的WebSocket服务器,并希望使用jetty WebSocketClient从另一个Java应用程序连接到它。这是我使用的测试方法:

@Test
public void testWebSockets() throws Exception {
    logger.debug("START testWebSockets()");
    WebSocketClientFactory factory = new WebSocketClientFactory();
    factory.setBufferSize(4096);
    factory.start();        

    WebSocketClient client = factory.newWebSocketClient();
    client.setMaxIdleTime(30000);
    client.setMaxTextMessageSize(1024);
    client.setProtocol("MyProtocol");                                       

    WebSocket webSocket = new ClientWebSocket();
    String wsUrl = "ws://localhost:8080/websocket";
    String wssUrl = "wss://localhost:8443/websocket";
    Future future = client.open(new URI(wssUrl), webSocket);
    WebSocket.Connection connection = (Connection) future.get(10, TimeUnit.SECONDS);
    assertNotNull("No connection!", connection);
    connection.sendMessage("TestMessage");
    connection.close();
    logger.debug("END testWebSockets()");
}

使用wsUrl不是问题,但是我无法使用wss。我不认为服务器是问题,因为我从javascript连接wss没有问题。我在这次测试中做错了什么?

服务器和客户端都在ssl设置上记录“错误记录MAC”。这是服务器日志的最后一部分:

    2013-01-15 10:26:06,398 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] unwrap OK NEED_TASK consumed=107 produced=0
2013-01-15 10:26:06,399 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NEED_TASK i/o/u=75/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1
,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NEED_TASK filled=69/75 flushed=0/0
2013-01-15 10:26:06,413 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NEED_UNWRAP i/o/u=75/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=
-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NEED_UNWRAP filled=0/75 flushed=0/0
2013-01-15 10:26:06,415 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] unwrap OK NEED_UNWRAP consumed=6 produced=0
2013-01-15 10:26:06,416 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NEED_UNWRAP i/o/u=69/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=
-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NEED_UNWRAP filled=0/69 flushed=0/0
2013-01-15 10:26:06,419 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] unwrap OK NEED_WRAP consumed=69 produced=0
2013-01-15 10:26:06,420 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NEED_WRAP i/o/u=0/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,
b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NEED_WRAP filled=0/0 flushed=0/0
2013-01-15 10:26:06,421 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] wrap OK NEED_WRAP consumed=0 produced=6
2013-01-15 10:26:06,422 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NEED_WRAP i/o/u=0/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,
b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NEED_WRAP filled=0/0 flushed=6/0
2013-01-15 10:26:06,423 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] wrap OK FINISHED consumed=0 produced=69
2013-01-15 10:26:06,424 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NOT_HANDSHAKING i/o/u=0/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0
,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NOT_HANDSHAKING filled=0/0 flushed=69/0
2013-01-15 10:26:06,426 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NOT_HANDSHAKING i/o/u=0/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0
,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NOT_HANDSHAKING filled=0/0 flushed=0/0
2013-01-15 10:26:06,428 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - [Session-1, SSL_NULL_WITH_NULL_NULL] SslConnection@9063ac SSL NOT_HANDSHAKING i/o/u=245/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s
=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} NOT_HANDSHAKING filled=245/245 flushed=0/0
2013-01-15 10:26:06,430 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ssl - SCEP@1dac071{l(/127.0.0.1:65084)<->r(/127.0.0.1:8444),d=true,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=1r}-{SslConnection@9063ac SSL NEED_WRAP
i/o/u=245/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0}}
javax.net.ssl.SSLException: bad record MAC
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1639)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1607)
        at sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:979)
        at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:884)
        at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:758)
        at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624)
        at org.eclipse.jetty.io.nio.SslConnection.unwrap(SslConnection.java:524)
        at org.eclipse.jetty.io.nio.SslConnection.process(SslConnection.java:359)
        at org.eclipse.jetty.io.nio.SslConnection.access$900(SslConnection.java:48)
        at org.eclipse.jetty.io.nio.SslConnection$SslEndPoint.fill(SslConnection.java:666)
        at org.eclipse.jetty.http.HttpParser.fill(HttpParser.java:1035)
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:280)
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
        at org.eclipse.jetty.io.nio.SslConnection.handle(SslConnection.java:196)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
        at java.lang.Thread.run(Thread.java:722)
2013-01-15 10:26:06,463 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ChannelEndPoint - close SCEP@1dac071{l(/127.0.0.1:65084)<->r(/127.0.0.1:8444),d=true,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=1!}-{SslConnection@906
3ac SSL NEED_WRAP i/o/u=245/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0}}
2013-01-15 10:26:06,469 qtp9902580-25 DEBUG org.eclipse.jetty.http.HttpParser -
javax.net.ssl.SSLException: bad record MAC
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1639)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1607)
        at sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:979)
        at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:884)
        at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:758)
        at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624)
        at org.eclipse.jetty.io.nio.SslConnection.unwrap(SslConnection.java:524)
        at org.eclipse.jetty.io.nio.SslConnection.process(SslConnection.java:359)
        at org.eclipse.jetty.io.nio.SslConnection.access$900(SslConnection.java:48)
        at org.eclipse.jetty.io.nio.SslConnection$SslEndPoint.fill(SslConnection.java:666)
        at org.eclipse.jetty.http.HttpParser.fill(HttpParser.java:1035)
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:280)
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
        at org.eclipse.jetty.io.nio.SslConnection.handle(SslConnection.java:196)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
        at java.lang.Thread.run(Thread.java:722)
2013-01-15 10:26:06,506 qtp9902580-25 DEBUG org.eclipse.jetty.http.HttpParser - HttpParser{s=-14,l=0,c=0}
org.eclipse.jetty.io.EofException
        at org.eclipse.jetty.http.HttpParser.fill(HttpParser.java:1041)
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:280)
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
        at org.eclipse.jetty.io.nio.SslConnection.handle(SslConnection.java:196)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
        at java.lang.Thread.run(Thread.java:722)
Caused by: javax.net.ssl.SSLException: bad record MAC
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1639)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1607)
        at sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:979)
        at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:884)
        at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:758)
        at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624)
        at org.eclipse.jetty.io.nio.SslConnection.unwrap(SslConnection.java:524)
        at org.eclipse.jetty.io.nio.SslConnection.process(SslConnection.java:359)
        at org.eclipse.jetty.io.nio.SslConnection.access$900(SslConnection.java:48)
        at org.eclipse.jetty.io.nio.SslConnection$SslEndPoint.fill(SslConnection.java:666)
        at org.eclipse.jetty.http.HttpParser.fill(HttpParser.java:1035)
        ... 9 more
2013-01-15 10:26:06,470 qtp9902580-23 Selector0 DEBUG org.eclipse.jetty.io.nio - destroyEndPoint SCEP@1dac071{l(null)<->r(0.0.0.0/0.0.0.0:8444),d=true,open=false,ishut=true,oshut=true,rb=false,wb=false,w=true,i=1!}-{SslConnection@9063ac
SSL NEED_WRAP i/o/u=245/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0}}
2013-01-15 10:26:06,555 qtp9902580-25 DEBUG org.eclipse.jetty.server.AsyncHttpConnection - Disabled read interest while writing response SSL NEED_WRAP i/o/u=245/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0
,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=0},r=0}
2013-01-15 10:26:06,559 qtp9902580-23 Selector0 DEBUG org.eclipse.jetty.server.AbstractHttpConnection - closed AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=0},r=0
2013-01-15 10:26:06,560 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio - EOF
org.eclipse.jetty.io.EofException
        at org.eclipse.jetty.http.HttpParser.fill(HttpParser.java:1041)
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:280)
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
        at org.eclipse.jetty.io.nio.SslConnection.handle(SslConnection.java:196)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:628)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:52)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
        at java.lang.Thread.run(Thread.java:722)
Caused by: javax.net.ssl.SSLException: bad record MAC
        at sun.security.ssl.Alerts.getSSLException(Alerts.java:208)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1639)
        at sun.security.ssl.SSLEngineImpl.fatal(SSLEngineImpl.java:1607)
        at sun.security.ssl.SSLEngineImpl.readRecord(SSLEngineImpl.java:979)
        at sun.security.ssl.SSLEngineImpl.readNetRecord(SSLEngineImpl.java:884)
        at sun.security.ssl.SSLEngineImpl.unwrap(SSLEngineImpl.java:758)
        at javax.net.ssl.SSLEngine.unwrap(SSLEngine.java:624)
        at org.eclipse.jetty.io.nio.SslConnection.unwrap(SslConnection.java:524)
        at org.eclipse.jetty.io.nio.SslConnection.process(SslConnection.java:359)
        at org.eclipse.jetty.io.nio.SslConnection.access$900(SslConnection.java:48)
        at org.eclipse.jetty.io.nio.SslConnection$SslEndPoint.fill(SslConnection.java:666)
        at org.eclipse.jetty.http.HttpParser.fill(HttpParser.java:1035)
        ... 9 more
2013-01-15 10:26:06,623 qtp9902580-25 DEBUG org.eclipse.jetty.io.nio.ChannelEndPoint - close SCEP@1dac071{l(null)<->r(0.0.0.0/0.0.0.0:8444),d=true,open=false,ishut=true,oshut=true,rb=false,wb=false,w=true,i=1-}-{SslConnection@9063ac SSL
NEED_WRAP i/o/u=245/0/0 ishut=false oshut=false {AsyncHttpConnection@17fc2ee,g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=0},r=0}}

1 个答案:

答案 0 :(得分:2)

我也尝试使用Jetty 9.0.3,但得到与Jetty - SSL Websocket Client

相同的例外

由于似乎jetty websocket客户端存在其他人没有的问题,我切换到async http client,这对我来说非常适合:

@Test
public void testNingWebSockets() throws Exception {
    logger.debug("START testNingWebSockets()");

    String wsUrl = "ws://localhost:8081/websocket";
    String wssUrl = "wss://localhost:8444/websocket";
    AsyncHttpClient client = new AsyncHttpClient();
    WebSocketUpgradeHandler handler = new WebSocketUpgradeHandler.Builder().addWebSocketListener(new ClientWebSocket()).build();
    WebSocket websocket = client.prepareGet(wssUrl).execute(handler).get();
    Assert.assertNotNull("No connection!", websocket);
    logger.debug("Connection made: " + websocket.isOpen());
    websocket.sendMessage("TestMessage".getBytes());
    Thread.sleep(1000);
    websocket.close();
    logger.debug("END testNingWebSockets()");
}