我有以下代码,应该在独立的Java应用程序(jnlp触发)中设置安全的websocket端点,并允许我从客户端(浏览器中的Javascript)与url连接: ws://本地主机:8444 /回波
服务器启动时没有错误,但我无法连接:
WebSocket连接到' wss:// localhost:8444 / echo'失败:错误 连接建立: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://)进行连接时,我可以成功连接到上面的内容。
答案 0 :(得分:1)
最后让它处理了大量的反复试验,一些痛苦以及这个家伙的一些帮助:https://stackoverflow.com/a/37882046/2288004
对于那些认为他们可能会得到帮助的人,我更新了服务器代码,如下所示,并开始创建自签名证书: 此外,最终的piècederésistance(使用自签名(不可信)证书时)是浏览所选浏览器中的安全URL,并收到提示,以便您可以添加例外。如果你不这样做,它将永远不会工作!!
final int port = 5040;
final int sslPort = 8442;
server = new Server(port);
SslContextFactory contextFactory = new SslContextFactory();
contextFactory.setKeyStorePath("/Users/me/keystore");
contextFactory.setKeyStorePassword("password");
SslConnectionFactory sslConnectionFactory = new SslConnectionFactory(contextFactory, org.eclipse.jetty.http.HttpVersion.HTTP_1_1.toString());
HttpConfiguration config = new HttpConfiguration();
config.setSecureScheme("https");
config.setSecurePort(sslPort);
config.setOutputBufferSize(32786);
config.setRequestHeaderSize(8192);
config.setResponseHeaderSize(8192);
HttpConfiguration sslConfiguration = new HttpConfiguration(config);
sslConfiguration.addCustomizer(new SecureRequestCustomizer());
HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(sslConfiguration);
ServerConnector connector = new ServerConnector(server, sslConnectionFactory, httpConnectionFactory);
connector.setPort(sslPort);
server.addConnector(connector);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
try
{
// Add websocket servlet
ServletHolder wsHolder = new ServletHolder("ws-echo", new EchoSocketServlet());
context.addServlet(wsHolder,"/echo");
server.setHandler(context);
server.start();
server.dump(System.err);
server.join();
}
catch (Throwable t)
{
t.printStackTrace(System.err);
}