Secure WebSocket / Jetty服务器(非嵌入式)/ HTTPS:服务不可用(503)

时间:2015-11-20 21:09:34

标签: java ssl websocket jetty

我有一个非常简单的WebSocket服务器来自Danny Cowards的“Java WebSocket编程书”,看起来像这样:

import javax.websocket.server.ServerEndpoint;
import javax.websocket.OnMessage;

@ServerEndpoint (value="/echo")
public class EchoServer
{
    @OnMessage
    public String echo (String incomingMessage)
    {
    return "I got this (" + incomingMessage + "), so I am sending it back!";
    }
}

我使用gradle 2.7编译它(参见下面的build.gradle

apply plugin: 'java'
apply plugin: "war"

repositories {
    mavenCentral()
}

dependencies {
        compile 'javax.websocket:javax.websocket-api:1.1'
}

并将其放在普通(仅限HTTP)Jetty服务器(webapps)的jetty-distribution-9.3.6.v20151106目录中,我可以让它与访问WebSocket接口的客户端进行通信,如下所示:

            var wsUri = "ws://localhost:8080/echoserver/echo";

然后,我使用相同的*.war文件,并将其放在安全的jetty服务器上(运行jetty-9.1.3.v20140225),但部署不起作用,我使用服务器访问(注意< em> wss now)

            var wsUri = "wss://localhost:8443/echoserver/echo";

无法解决错误。来自chrome上的JavaScript控制台的特定错误是'WebSocket connection to 'wss://localhost:8443/echoserver/echo' failed: Error during WebSocket handshake: Unexpected Response code: 503'。但我认为这是因为有一个RunTimeException扫描它发生的EchoServer.class(如在jetty stderrout.log中所示):

2015-11-20 15:32:14.320:INFO:oejs.Server:main: jetty-9.1.3.v20140225
2015-11-20 15:32:14.329:INFO:oejs.AbstractNCSARequestLog:main: Opened /opt/jetty/logs/2015_11_20.request.log
2015-11-20 15:32:14.329:INFO:oejs.AbstractNCSARequestLog:main: Opened /opt/jetty/logs/2015_11_20.request.log
2015-11-20 15:32:14.331:INFO:oejdp.ScanningAppProvider:main: Deployment monitor [file:/opt/jetty/webapps/] at interval 1
2015-11-20 15:32:14.421:INFO:oeja.AnnotationConfiguration:main: Scanned 1 container path jars, 1 WEB-INF/lib jars, 1 WEB-INF/classes d
irs in 17ms for context o.e.j.w.WebAppContext@4c70fda8{/echoserver,file:/tmp/jetty-0.0.0.0-8443-echoserver.war-_echoserver-any-7817985
952118790015.dir/webapp/,STARTING}{/echoserver.war}
2015-11-20 15:32:14.421:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.w.WebAppContext@4c70fda8{/echoserver,file:/tmp/j
etty-0.0.0.0-8443-echoserver.war-_echoserver-any-7817985952118790015.dir/webapp/,STARTING}{/echoserver.war}
java.lang.RuntimeException: Error scanning file EchoServer.class
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:705)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:686)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:686)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:686)
    at org.eclipse.jetty.annotations.AnnotationParser.parse(AnnotationParser.java:821)
    at org.eclipse.jetty.annotations.AnnotationConfiguration$ParserTask.call(AnnotationConfiguration.java:159)
    at org.eclipse.jetty.annotations.AnnotationConfiguration$1.run(AnnotationConfiguration.java:542)
    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)
Caused by: 
java.lang.IllegalArgumentException
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.objectweb.asm.ClassReader.<init>(Unknown Source)
    at org.eclipse.jetty.annotations.AnnotationParser.scanClass(AnnotationParser.java:970)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:700)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:686)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:686)
    at org.eclipse.jetty.annotations.AnnotationParser.parseDir(AnnotationParser.java:686)
    at org.eclipse.jetty.annotations.AnnotationParser.parse(AnnotationParser.java:821)
    at org.eclipse.jetty.annotations.AnnotationConfiguration$ParserTask.call(AnnotationConfiguration.java:159)
    at org.eclipse.jetty.annotations.AnnotationConfiguration$1.run(AnnotationConfiguration.java:542)
    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)

我的环境是Ubuntu 14.04 / x64 / Oracle java 8.顺便说一句,我从网站下载了Danny Cowards的源代码,它与我的代码完全相同。然而,它带有它自己的神秘* .war(在dist目录中,但是ant build.xml显然是指所有没有业务存在的NetBeans依赖项,因此我无法使用它编译它ant)并将其放在HTTPS jetty中,它完全按预期运行,当我使用wss:访问它时,我不会收到错误,因此它不是SSL /自签名证书问题,或者我认为。

谁能告诉我这里可能出现什么问题?对此有任何帮助深表感谢。

谢谢, 桑尼。

PS:我已经在SO上检查了一些WebSocket / SSL / Jetty帖子,但它们似乎没有解决我对Jetty的问题。

1 个答案:

答案 0 :(得分:1)

Jetty 9.1.x对于JSR356(javax.websocket)来说太旧了。

使用Jetty 9.2.x或Jetty 9.3.x进行JSR356支持。

由于您似乎使用的是Java 8,因此请使用版本Jetty版本9.3.6.v20151106