【发布时间】:2017-02-16 18:33:36
【问题描述】:
我有以下代码,它应该在独立的 Java 应用程序(触发 jnlp)中设置一个安全的 websocket 端点,并允许我从客户端(浏览器中的 Javascript)使用 url 进行连接:ws://本地主机:8444/echo
服务器启动没有错误,但我无法连接:
到 'wss://localhost:8444/echo' 的 WebSocket 连接失败:错误 连接建立:net::ERR_CONNECTION_CLOSED
server = new Server();
SslContextFactory contextFactory = new SslContextFactory();
contextFactory.setKeyStorePath("/path/keys/keystore.jks");
contextFactory.setKeyStorePassword("changeit");
SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(contextFactory, org.eclipse.jetty.http.HttpVersion.HTTP_1_1.toString());
ServerConnector connector = new ServerConnector(server, sslConnectionFactory);
connector.setPort(8444);
ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
contextHandler.setContextPath("/");
HandlerCollection hc = new HandlerCollection();
hc.addHandler(contextHandler);
server.setHandler(contextHandler);
server.addConnector(connector);
// Add websocket servlet
ServletHolder wsHolder = new ServletHolder("echo",new EchoSocketServlet());
contextHandler.addServlet(wsHolder,"/echo");
try
{
server.start();
server.join();
}
catch (Exception e)
{
e.printStackTrace();
}
如果有人能在上述内容中发现明显的错误,我们将不胜感激。 此外,当我删除 SslConnectionFactory 并使用非 SSL Websocket URL (ws://) 连接时,我可以成功连接到上述内容。
【问题讨论】:
-
如果您尝试连接到
https://localhost:8444/blarg之类的东西,您会收到更有用的错误消息吗? -
HandlerCollection hc未使用,请随意删除。 -
尝试了浏览器中建议的 URL 并得到以下结果: curl: (35) 服务器中止 SSL 握手 我原以为上面的代码中使用了 hc,因为contextHandler 被添加到它,然后作为处理程序添加到服务器对象?
标签: java ssl websocket embedded-jetty jetty-9