【问题标题】:Cannot connect to secure Websocket running on Jetty无法连接到在 Jetty 上运行的安全 Websocket
【发布时间】: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


【解决方案1】:

经过大量的反复试验、一些痛苦和这个家伙的一点帮助,终于让它工作了:https://stackoverflow.com/a/37882046/2288004

对于任何认为他们可能会因此得到帮助的人,我更新了服务器代码,如下所示,并开始创建自签名证书: 此外,最后一点(当使用自签名(不受信任)证书时)是在您选择的浏览器中浏览到安全 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);
    }

【讨论】:

    猜你喜欢
    • 2016-08-12
    • 2019-07-17
    • 2014-07-09
    • 2023-04-04
    • 2015-08-03
    • 2015-05-25
    • 1970-01-01
    • 2014-10-27
    • 2019-09-27
    相关资源
    最近更新 更多