jetty - 使用 maven-jetty-plugin 启用 HTTP2

标签 jetty maven-jetty-plugin jetty-9 http2

我已经使用 jetty 通过 SSL 启用了 HTTP/2 连接器。当我尝试连接浏览器时,出现“ERR_SSL_PROTOCOL_ERROR”错误。如果我切换到 HTTP/1.1 连接器,一切正常。

这是我的 jetty 配置文件:

<!-- ============================================================= -->
<!-- Configure the Jetty Server instance with an ID "Server"       -->
<!-- by adding a HTTP connector.                                   -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

    <New id="httpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
        <Set name="secureScheme">https</Set>
        <Set name="securePort"><Property name="jetty.secure.port" default="8443" /></Set>
        <Set name="outputBufferSize">32768</Set>
        <Set name="requestHeaderSize">8192</Set>
        <Set name="responseHeaderSize">8192</Set>
        <Set name="sendServerVersion">true</Set>
        <Set name="sendDateHeader">false</Set>
        <Set name="headerCacheSize">512</Set>

        <!-- Uncomment to enable handling of X-Forwarded- style headers
        <Call name="addCustomizer">
          <Arg><New class="org.eclipse.jetty.server.ForwardedRequestCustomizer"/></Arg>
        </Call>
        -->
    </New>

    <!-- =========================================================== -->
    <!-- Add a HTTP Connector.                                       -->
    <!-- Configure an o.e.j.server.ServerConnector with a single     -->
    <!-- HttpConnectionFactory instance using the common httpConfig  -->
    <!-- instance defined in jetty.xml                               -->
    <!--                                                             -->
    <!-- Consult the javadoc of o.e.j.server.ServerConnector and     -->
    <!-- o.e.j.server.HttpConnectionFactory for all configuration    -->
    <!-- that may be set here.                                       -->
    <!-- =========================================================== -->
    <Call name="addConnector">
        <Arg>
            <New id="httpConnector" class="org.eclipse.jetty.server.ServerConnector">
                <Arg name="server"><Ref refid="Server" /></Arg>
                <Arg name="acceptors" type="int"><Property name="http.acceptors" default="-1"/></Arg>
                <Arg name="selectors" type="int"><Property name="http.selectors" default="-1"/></Arg>
                <Arg name="factories">
                    <Array type="org.eclipse.jetty.server.ConnectionFactory">
                        <!-- uncomment to support proxy protocol
                    <Item>
                          <New class="org.eclipse.jetty.server.ProxyConnectionFactory"/>
                        </Item>-->
                        <Item>
                            <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                                <Arg name="config"><Ref refid="httpConfig" /></Arg>
                            </New>
                        </Item>
                    </Array>
                </Arg>
                <Set name="host"><Property name="jetty.host" /></Set>
                <Set name="port"><Property name="jetty.port" default="8080" /></Set>
                <Set name="idleTimeout"><Property name="http.timeout" default="30000"/></Set>
                <Set name="soLingerTime"><Property name="http.soLingerTime" default="-1"/></Set>
                <Set name="acceptorPriorityDelta"><Property name="http.acceptorPriorityDelta" default="0"/></Set>
                <Set name="selectorPriorityDelta"><Property name="http.selectorPriorityDelta" default="0"/></Set>
                <Set name="acceptQueueSize"><Property name="http.acceptQueueSize" default="0"/></Set>
            </New>
        </Arg>
    </Call>

</Configure>

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- ============================================================= -->
<!-- Configure a HTTPS connector.                                  -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- and jetty-ssl.xml.                                            -->
<!-- ============================================================= -->
<Configure id="sslConnector" class="org.eclipse.jetty.server.ServerConnector">

    <!--Call name="addIfAbsentConnectionFactory">
        <Arg>
            <New class="org.eclipse.jetty.server.SslConnectionFactory">
                <Arg name="next">http/1.1</Arg>
                <Arg name="sslContextFactory"><Ref refid="sslContextFactory"/></Arg>
            </New>
        </Arg>
    </Call>

    <Call name="addConnectionFactory">
        <Arg>
            <New class="org.eclipse.jetty.server.HttpConnectionFactory">
                <Arg name="config"><Ref refid="sslHttpConfig" /></Arg>
            </New>
        </Arg>
    </Call-->

    <!-- ============================================================= -->
    <!-- Configure a HTTP2 on the ssl connector.                       -->
    <!-- ============================================================= -->
    <Call name="addConnectionFactory">
        <Arg>
            <New class="org.eclipse.jetty.http2.server.HTTP2ServerConnectionFactory">
                <Arg name="config"><Ref refid="sslHttpConfig"/></Arg>
                <Set name="maxConcurrentStreams"><Property name="http2.maxConcurrentStreams" default="1024"/></Set>
            </New>
        </Arg>
    </Call>

</Configure>

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- ============================================================= -->
<!-- Base SSL configuration                                        -->
<!-- This configuration needs to be used together with 1 or more   -->
<!-- of jetty-https.xml or jetty-http2.xml                         -->
<!-- ============================================================= -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">

    <!-- =========================================================== -->
    <!-- Create a TLS specific HttpConfiguration based on the        -->
    <!-- common HttpConfiguration defined in jetty.xml               -->
    <!-- Add a SecureRequestCustomizer to extract certificate and    -->
    <!-- session information                                         -->
    <!-- =========================================================== -->
    <New id="sslHttpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
        <Arg><Ref refid="httpConfig"/></Arg>
        <Call name="addCustomizer">
            <Arg><New class="org.eclipse.jetty.server.SecureRequestCustomizer"/></Arg>
        </Call>
    </New>

    <!-- =========================================================== -->
    <!-- Add a SSL Connector with no protocol factories              -->
    <!-- =========================================================== -->
    <Call  name="addConnector">
        <Arg>
            <New id="sslConnector" class="org.eclipse.jetty.server.ServerConnector">
                <Arg name="server"><Ref refid="Server" /></Arg>
                <Arg name="acceptors" type="int"><Property name="ssl.acceptors" default="-1"/></Arg>
                <Arg name="selectors" type="int"><Property name="ssl.selectors" default="-1"/></Arg>
                <Arg name="factories">
                    <Array type="org.eclipse.jetty.server.ConnectionFactory">
                        <!-- uncomment to support proxy protocol
                    <Item>
                          <New class="org.eclipse.jetty.server.ProxyConnectionFactory"/>
                        </Item>-->
                    </Array>
                </Arg>

                <Set name="host"><Property name="jetty.host" /></Set>
                <Set name="port"><Property name="ssl.port" default="443" /></Set>
                <Set name="port"><Property name="port" default="9090" /></Set>
                <Set name="idleTimeout"><Property name="ssl.timeout" default="30000"/></Set>
                <Set name="soLingerTime"><Property name="ssl.soLingerTime" default="-1"/></Set>
                <Set name="acceptorPriorityDelta"><Property name="ssl.acceptorPriorityDelta" default="0"/></Set>
                <Set name="selectorPriorityDelta"><Property name="ssl.selectorPriorityDelta" default="0"/></Set>
                <Set name="acceptQueueSize"><Property name="ssl.acceptQueueSize" default="0"/></Set>
            </New>
        </Arg>
    </Call>

    <!-- ============================================================= -->
    <!-- Create a TLS (SSL) Context Factory  for later reuse           -->
    <!-- ============================================================= -->
    <New id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory">
        <Set name="KeyStorePath"><Property name="jetty.base" default="../" />/<Property name="jetty.keystore" default="keystore.jks"/></Set>
        <Set name="KeyStorePassword"><Property name="jetty.keystore.password" default="storepwd"/></Set>
        <Set name="KeyManagerPassword"><Property name="jetty.keymanager.password" default="storepwd"/></Set>
        <Set name="TrustStorePath"><Property name="jetty.base" default="../" />/<Property name="jetty.truststore" default="truststore.jks"/></Set>
        <Set name="TrustStorePassword"><Property name="jetty.truststore.password" default="storepwd"/></Set>
        <Set name="EndpointIdentificationAlgorithm"></Set>
        <Set name="NeedClientAuth"><Property name="jetty.ssl.needClientAuth" default="false"/></Set>
        <Set name="WantClientAuth"><Property name="jetty.ssl.wantClientAuth" default="false"/></Set>
        <Set name="ExcludeCipherSuites">
            <Array type="String">
                <Item>SSL_RSA_WITH_DES_CBC_SHA</Item>
                <Item>SSL_DHE_RSA_WITH_DES_CBC_SHA</Item>
                <Item>SSL_DHE_DSS_WITH_DES_CBC_SHA</Item>
                <Item>SSL_RSA_EXPORT_WITH_RC4_40_MD5</Item>
                <Item>SSL_RSA_EXPORT_WITH_DES40_CBC_SHA</Item>
                <Item>SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA</Item>
                <Item>SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA</Item>
            </Array>
        </Set>
    </New>
</Configure>

我正在使用 jetty-server、http2-server 和 jetty-alpn-server 工件版本 9.3.0.M1,是否需要添加任何其他依赖项?我正在使用JDK7。

谢谢

最佳答案

它终于适用于 jetty 9.3.0!我们需要确保 ALPN 配置良好并且我们使用 JDK8。

这是我为 maven-jetty-plugin 配置的内容:

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <verbose>true</verbose>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-maven-plugin</artifactId>
            <version>${jetty-version}</version>
            <configuration>
                <webAppSourceDirectory>${project.build.directory}/${project.name}</webAppSourceDirectory>
                <systemProperties>
                    <force>true</force>
                </systemProperties>
                <scanIntervalSeconds>10</scanIntervalSeconds>
                <webAppConfig>
                    <contextPath>/</contextPath>
                </webAppConfig>
                <jettyXml>../jetty.xml,../jetty-ssl.xml,../jetty-https.xml</jettyXml>
                <jvmArgs>-Xbootclasspath/p:${settings.localRepository}/org/mortbay/jetty/alpn/alpn-boot/${alpn-version}/alpn-boot-${alpn-version}.jar</jvmArgs>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.eclipse.jetty.http2</groupId>
                    <artifactId>http2-server</artifactId>
                    <version>${jetty-version}</version>
                </dependency>
                <dependency>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-alpn-server</artifactId>
                    <version>${jetty-version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

<properties>
    <jetty-version>9.3.0.M2</jetty-version>
    <alpn-version>8.1.0.v20141016</alpn-version>
</properties>

根据JDK版本选择ALPN工件版本:http://eclipse.org/jetty/documentation/current/alpn-chapter.html

我还在 HTTP2ServerConnectionFactory 之前添加了这两个 ConnectioFactory
<Call name="addConnectionFactory">
    <Arg>
        <New class="org.eclipse.jetty.server.SslConnectionFactory">
            <Arg name="next">alpn</Arg>
            <Arg name="sslContextFactory"><Ref refid="sslContextFactory"/></Arg>
        </New>
    </Arg>
</Call>

<Call name="addConnectionFactory">
    <Arg>
        <New id="alpn" class="org.eclipse.jetty.alpn.server.ALPNServerConnectionFactory">
            <Arg type="String">
                <Property name="alpn.protocols" default="" />
            </Arg>
            <Set name="defaultProtocol">
                <Property name="alpn.defaultProtocol" />
            </Set>
        </New>
    </Arg>
</Call>

关于jetty - 使用 maven-jetty-plugin 启用 HTTP2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389690/

相关文章:

linux - 让 maven 在 Linux 中的端口 80 jetty 上运行 jetty

java - jetty 9随机时间无异常关闭

ssl - 如何将 Windows 证书存储与 Jetty 一起使用?

java - 如何在 Jetty 上的 Spring 应用程序中将 jsessionid cookie 路径更改为服务器根目录?

java - Maven Jetty 插件中的 Jetty JNDI 错误

java - 无法在 Ubuntu 中将 Jetty9 安装为服务

apache - Jetty Solr 出现错误?

使用 ScalatraSuite (FunSuiteLike) 进行独立 sbt 项目的 Scalatra 测试失败

tomcat - 一个 grails 应用程序有多个或多个域?

java - 如何告诉 jetty 将部分 jar 文件提取到其上下文临时位置