没有SSL下一个协议的协议工厂:ServerConnector中的“ HTTP / 1.1”

时间:2018-10-21 15:04:38

标签: jetty

在创建这样的Jetty ServerConnector时:

Server server = new Server();
SslContextFactory sslContextFactory = new SslContextFactory();

ServerConnector sslConnector = new ServerConnector(server,
    new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()));

启动服务器时出现以下错误:

java.lang.IllegalStateException: No protocol factory for SSL next protocol: 'HTTP/1.1' in ServerConnector@37918c79{SSL,[ssl]}{0.0.0.0:7443}
    at org.eclipse.jetty.server.AbstractConnector.doStart(AbstractConnector.java:278)
    at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:81)
    at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:235)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.server.Server.doStart(Server.java:395)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    [...]

1 个答案:

答案 0 :(得分:2)

您缺少组成SSL + HTTP(又称HTTPS)的连接的HTTP部分。

  

提示:如果没有看到HttpConfigurationHttpConnectionFactory传递到ServerConnector中,则说明您没有将该ServerConnector用于HTTP。 (代理,unixsocket,jni连接器,自定义连接器等都属于这种情况)

典型的设置是...

    int httpsPort = 8443;

    HttpConfiguration http_config = new HttpConfiguration();
    http_config.setSecureScheme("https");
    http_config.setSecurePort(httpsPort);

    // TODO: Setup non-SSL/TLS Connector/Port here (using http_config) and
    // set it to auto-redirect to SSL/TLS port.

    // SSL Context Factory
    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStorePath("/path/to/keystore");
    sslContextFactory.setKeyStorePassword(...);
    sslContextFactory.setKeyManagerPassword(...);
    sslContextFactory.setTrustStorePath("/path/to/keystore");
    sslContextFactory.setTrustStorePassword(...);

    // SSL HTTP Configuration
    HttpConfiguration https_config = new HttpConfiguration(http_config);
    https_config.addCustomizer(new SecureRequestCustomizer()); // so that servlets can see the
                                                               // encryption details

    // SSL Connector
    ServerConnector sslConnector = new ServerConnector(server,
        new SslConnectionFactory(sslContextFactory,HttpVersion.HTTP_1_1.asString()),
        new HttpConnectionFactory(https_config)); // <-- the argument you were missing
    sslConnector.setPort(httpsPort);
    server.addConnector(sslConnector);