无法连接到 Jetty 上的安全 Websocket 运行
Cannot connect to secure Websocket running on Jetty
我有以下代码应该在独立的 Java 应用程序中设置安全的 websocket 端点(jnlp 触发)并允许我从客户端连接(Java浏览器中的脚本)使用 url:ws://localhost:8444/echo
服务器启动没有错误,但我无法连接:
WebSocket connection to 'wss://localhost:8444/echo' failed: Error in
connection establishment: 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://).
连接时,我可以成功连接到上述内容
经过大量的试验和错误、一些痛苦和这个人的一点帮助,终于让它工作了:
对于那些认为他们可能会因此得到帮助的人,我更新了服务器代码如下,并创建了一个自签名证书:
此外,最后的 pièce de ré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);
}
我有以下代码应该在独立的 Java 应用程序中设置安全的 websocket 端点(jnlp 触发)并允许我从客户端连接(Java浏览器中的脚本)使用 url:ws://localhost:8444/echo
服务器启动没有错误,但我无法连接:
WebSocket connection to 'wss://localhost:8444/echo' failed: Error in connection establishment: 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://).
连接时,我可以成功连接到上述内容经过大量的试验和错误、一些痛苦和这个人的一点帮助,终于让它工作了:
对于那些认为他们可能会因此得到帮助的人,我更新了服务器代码如下,并创建了一个自签名证书: 此外,最后的 pièce de ré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);
}