jetty支持http2从jetty9.3版本开始。
1、下载jetty并解压到目录
jetty官网:https://www.eclipse.org/jetty/download.html
解压到目录,如/data/jetty443
[root@test data]# cd jetty443/
[root@test jetty443]# ll
total 736
drwxr-xr-x. 2 root root 4096 Aug 31 11:05 bin
drwxr-xr-x. 7 root root 4096 Aug 31 11:06 demo-base
drwxr-xr-x. 5 root root 4096 Aug 31 15:01 etc
-rw-r--r--. 1 root root 135 Sep 3 14:15 jetty443.state
drwxr-xr-x. 15 root root 4096 Aug 31 11:25 lib
-rw-r--r--. 1 root root 30012 Aug 31 11:05 license-eplv10-aslv20.html
drwxr-xr-x. 3 root root 4096 Aug 31 11:09 logs
drwxr-xr-x. 22 root root 4096 Aug 31 11:06 modules
-rw-r--r--. 1 root root 6262 Aug 31 11:05 notice.html
-rw-r--r--. 1 root root 1637 Aug 31 11:05 README.TXT
drwxr-xr-x. 2 root root 4096 Aug 31 11:06 resources
drwxr-xr-x. 2 root root 4096 Sep 3 13:57 start.d
-rw-r--r--. 1 root root 147476 Aug 31 11:05 start.jar
-rw-r--r--. 1 root root 504664 Aug 31 11:05 VERSION.txt
drwxr-xr-x. 2 root root 4096 Aug 31 11:09 webapps
[root@test jetty443]#
2、配置激活jetty模块
进入jetty目录/data/jetty443,执行命令:
java -jar start.jar --add-to-startd=http,https,http2,http2c,deploy,alpn
激活如下模块:
[root@test jetty443]# ll start.d/
total 28
-rw-r--r--. 1 root root 477 Sep 3 13:57 alpn.ini
-rw-r--r--. 1 root root 427 Aug 31 15:10 http2c.ini
-rw-r--r--. 1 root root 528 Aug 31 15:10 http2.ini
-rw-r--r--. 1 root root 175 Aug 31 15:10 https.ini
-rw-r--r--. 1 root root 3122 Aug 31 15:10 ssl.ini
-rw-r--r--. 1 root root 5371 Aug 31 15:10 start.ini
[root@test jetty443]#
3、修改端口
http端口:start.ini文件jetty.http.port=80
https端口:ssl.ini文件jetty.ssl.port=443
获取修改文件jetty-http.xml和jetty-ssl.xml文件的端口默认值。
4、配置证书
一般证书机构生成的证书有crt、key、pem、pfx几个文件。
如果是个人测试则直接用openssl或keytool正常jetty配置的keystore证书。
如果没有pfx证书,则通过如下命令生成:
openssl pkcs12 -export -out test.pfx -inkey test.key -in test.pem
pfx证书转成keystore:
keytool -importkeystore -srckeystore test.pfx -destkeystore jetty.jks -srcstoretype PKCS12 -deststoretype JKS
执行该命令是需要输入证书密码。
5、配置ssl
打开jetty-ssl-context.xml,修改KeyStorePath、TrustStorePath的证书路径:
<Set name="KeyStorePath"><Property name="jetty.base" default="." />/<Property name="jetty.sslContext.keyStorePath" deprecated="jetty.keystore" default="etc/cert/jetty.jks"/></Set>
<Set name="KeyManagerPassword"><Property name="jetty.sslContext.keyManagerPassword" deprecated="jetty.keymanager.password" default="OBF:19iy19j019j219j419j619j8"/></Set>
修改KeyStorePassword、KeyManagerPassword、 TrustStorePassword密码为证书的密码。
<Set name="KeyStorePassword"><Property name="jetty.sslContext.keyStorePassword" deprecated="jetty.keystore.password" default="OBF:19iy19j019j219j419j619j8"/></Set>
<Set name="KeyManagerPassword"><Property name="jetty.sslContext.keyManagerPassword" deprecated="jetty.keymanager.password" default="OBF:19iy19j019j219j419j619j8"/></Set>
<Set name="TrustStorePassword"><Property name="jetty.sslContext.trustStorePassword" deprecated="jetty.truststore.password" default="OBF:19iy19j019j219j419j619j8"/></Set>
此处default值可直接输入证书的密码,如123456,也可以使用OBF格式的keystore密码,转换命令:
java -cp jetty-all-9.0.5.v20130815.jar org.eclipse.jetty.util.security.Password yoursslpassword
完成以上配置后可启动jetty,https及http2正常配置,可看到启动日志:
2018-09-03 08:01:22.899:INFO:oejs.AbstractConnector:main: Started ServerConnector@f451970{SSL,[ssl, alpn, h2, http/1.1]}{0.0.0.0:443}
2018-09-03 08:01:22.900:INFO:oejs.AbstractConnector:main: Started ServerConnector@4a7c8cb7{HTTP/1.1,[http/1.1, h2c]}{0.0.0.0:80}
表示配置成功,通过chrome浏览器访问url,右键-检查(或者 更多工具-开发者工具)查看network页面,可看到协议。
6、增强ssl
通过以上配置后如果进行ssl server test(https://www.ssllabs.com/ssltest/analyze.html),会提示:
1、This server supports weak Diffie-Hellman (DH) key exchange parameters
2、The server does not support Forward Secrecy with the reference browsers.
导致不能达到A的评分。
主要原因是存在比较弱的加密算法引起。
解决办法,指定采用保密性更好的ECDHE系列算法:
jetty-ssl-context.xml增加如下配置:
<!-- Eliminate Old / Insecure / Anonymous Ciphers -->
<Call name="addExcludeCipherSuites">
<Arg>
<Array type="String">
<Item>.*NULL.*</Item>
<Item>.*RC4.*</Item>
<Item>.*MD5.*</Item>
<Item>.*DES.*</Item>
<Item>.*DSS.*</Item>
</Array>
</Arg>
</Call>
<!-- Enable Forward Secrecy Ciphers.
Note: this replaces the default Include Cipher list -->
<Set name="IncludeCipherSuites">
<Array type="String">
<Item>TLS_ECDHE.*</Item>
</Array>
</Set>