如何正确地使 Jetty WebSocketServlet 使用 HTTPS?

How to correctly make Jetty WebSocketServlet work with HTTPS?

我有一个 Jetty WebServlet,可以连接用 C# 和 android 编写的各种客户端。这目前可以使用简单的 HTTP,但我有兴趣将其升级到 HTTPS。为了尝试这样做,我正在创建这样的服务器:

public static void main(String[] args){

    SslContextFactory contextFactory = new SslContextFactory();
    contextFactory.setKeyStorePath("keystore.jks");
    contextFactory.setKeyStorePassword("********");
    SslConnectionFactory connectionFactory = new SslConnectionFactory(contextFactory, org.eclipse.jetty.http.HttpVersion.HTTP_2_0.toString());

    Server server = new Server(8080);

    ServerConnector connector = new ServerConnector(server, connectionFactory);
    connector.setPort(8443);
    server.addConnector(connector);

    ServletContextHandler servletCH = new ServletContextHandler();
    servletCH.setContextPath("/");
    servletCH.addServlet(ScheduleWebSocketServlet.class, "/schedule");

    server.setHandler(servletCH);

    server.start();
    server.join();


}

这似乎是错误的。 ScheduleWebSocketServletclass如下:

@WebServlet(name = "Schedule WebSocketServlet", urlPatterns = {"/schedule"})
public static class ScheduleWebSocketServlet extends WebSocketServlet{

    private static final long serialVersionUID = 5838283767965540728L;

    public void doGet(HttpServletRequest request, HttpServletResponse response){
        try {
            response.getWriter().println("<h1>Hello World</h1>");
        } catch (IOException e) {
            Main.LogError(e);
        }
    }

    @Override
    public void configure(WebSocketServletFactory arg0) {
        arg0.register(ScheduleWebSocket.class);
    }

}

所以我的问题是通过 HTTPS 使用 WebServlet 的正确方法是什么?

非常感谢您的帮助

好的,我的问题原来是我必须创建具有 http 和 https 配置的 ServerConnections,为它们提供详细信息并将它们分配给服务器。毕竟我只需要启动它们,这应该在 服务器启动

之后完成
    Server server = new Server();

    HttpConfiguration http_config = new HttpConfiguration();
    http_config.setSecureScheme("https");
    http_config.setSecurePort(8443);
    http_config.setOutputBufferSize(32768);

    HttpConfiguration https_config = new HttpConfiguration();
    https_config.addCustomizer(new SecureRequestCustomizer());

    ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(http_config));
    http.setPort(8080);
    http.setIdleTimeout(30000);

    SslContextFactory contextFactory = new SslContextFactory();
    contextFactory.setKeyStorePath("keystore.jks");
    contextFactory.setKeyStorePassword("changeit");
    SslConnectionFactory connectionFactory = new SslConnectionFactory(contextFactory, org.eclipse.jetty.http.HttpVersion.HTTP_1_1.toString());

    ServerConnector https = new ServerConnector(server, connectionFactory, new HttpConnectionFactory(https_config));
    https.setPort(8443);
    https.setIdleTimeout(50000);

    server.setConnectors(new Connector[]{http, https});

    ServletContextHandler servletCH = new ServletContextHandler();
    servletCH.setContextPath("/");
    servletCH.addServlet(ScheduleWebSocketServlet.class, "/schedule");

    server.setHandler(servletCH);

    server.start();
    http.start();
    https.start();
    server.join();