在Jetty服务器上安装SSL证书教程

[复制链接]
查看: 763|回复: 0

14

主题

14

帖子

54

积分

注册会员

Rank: 2

积分
54
发表于 2020-1-15 17:51:51 | 显示全部楼层 |阅读模式
在Jetty服务器上安装SSL证书教程:
首先你得有SSL证书,没有的可以到腾讯云和阿里云申请免费SSL证书(参考:阿里云、腾讯云免费SSL域名证书申请教程),或者购买SSL证书。


1、Jetty服务器版本确认。建议使用Jetty 9.2.22及以上版本。
2、从阿里云下载tomcat格式的证书。非系统生成的CSR需要生成pfx证书密匙对文件,转换命令如下。
  1. openssl pkcs12 -export -out 214362464370691.pfx -inkey 214362464370691.key -in 214362464370691.pem
复制代码
3、转换pfx的证书密匙对文件为jks格式,转换命令如下:
(说明 Windows环境注意在%JAVA_HOME%/jdk/bin目录中执行。)
  1. keytool -importkeystore -srckeystore 密匙对文件.pfx -destkeystore 证书名称.jks -srcstoretype PKCS12 -deststoretype JKS
复制代码
回车后输入两次要设置的jks格式证书密码,然后输入一次pfx证书密码。三次密码必须输入pfx-password.txt记录的密码。jks密码与pfx证书密码相同,否则可能会导致Jetty服务器启动失败。
4、配置Jetty的SSL。
确保Jetty的http页面可正常访问。


拷贝证书。进入Jetty服务器目录下的etc,新建存放jks格式证书的目录,并复制jks格式证书至当前目录
  1. # pwd
  2. /opt/jetty9222/etc
  3. # mkdir cert
  4. # cd cert/
  5. # cp ../../../keys/jetty.jks .
  6. # ls
  7. jetty.jks
复制代码
编辑Jetty服务器目录中的etc中的jetty-ssl.xml文件,设置证书相关参数(密码设置均为pfx-password.txt所记录的密码)。

  1. <?xml version="1.0"?>
  2. <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
  3. <!-- ============================================================= -->
  4. <!-- Configure a TLS (SSL) Context Factory                         -->
  5. <!-- This configuration must be used in conjunction with jetty.xml -->
  6. <!-- and either jetty-https.xml or jetty-spdy.xml (but not both)   -->
  7. <!-- ============================================================= -->
  8. <Configure id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory">
  9.   <Set name="KeyStorePath"><Property name="jetty.base" default="." />/<Property name="jetty.keystore" default="etc/cert/jetty.jks"/></Set>
  10.   <Set name="KeyStorePassword"><Property name="jetty.keystore.password" default="214362464370691"/></Set>
  11. <?xml version="1.0"?>
  12. <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
  13. <!-- ============================================================= -->
  14. <!-- Configure a TLS (SSL) Context Factory                         -->
  15. <!-- This configuration must be used in conjunction with jetty.xml -->
  16. <!-- and either jetty-https.xml or jetty-spdy.xml (but not both)   -->
  17. <!-- ============================================================= -->
  18. <Configure id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory">
  19.   <Set name="KeyStorePath"><Property name="jetty.base" default="." />/<Property name="jetty.keystore" default="etc/cert/jetty.jks"/></Set>
  20.   <Set name="KeyStorePassword"><Property name="jetty.keystore.password" default="214362464370691"/></Set>
  21.   <Set name="KeyManagerPassword"><Property name="jetty.keymanager.password" default="214362464370691"/></Set>
  22.   <Set name="TrustStorePath"><Property name="jetty.base" default="." />/<Property name="jetty.truststore" default="etc/cert/jetty.jks"/></Set>
  23.   <Set name="TrustStorePassword"><Property name="jetty.truststore.password" default="214362464370691"/></Set>
  24.   <Set name="EndpointIdentificationAlgorithm"></Set>
  25.   <Set name="NeedClientAuth"><Property name="jetty.ssl.needClientAuth" default="false"/></Set>
  26.   <Set name="WantClientAuth"><Property name="jetty.ssl.wantClientAuth" default="false"/></Set>
  27.   <Set name="ExcludeCipherSuites">
  28.     <Array type="String">
  29.       <Item>SSL_RSA_WITH_DES_CBC_SHA</Item>
  30.       <Item>SSL_DHE_RSA_WITH_DES_CBC_SHA</Item>
  31.       <Item>SSL_DHE_DSS_WITH_DES_CBC_SHA</Item>
  32.       <Item>SSL_RSA_EXPORT_WITH_RC4_40_MD5</Item>
  33.       <Item>SSL_RSA_EXPORT_WITH_DES40_CBC_SHA</Item>
  34.       <Item>SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA</Item>
  35.       <Item>SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA</Item>
  36.     </Array>
  37.   </Set>
  38.   <!-- =========================================================== -->
  39.   <!-- Create a TLS specific HttpConfiguration based on the        -->
  40.   <!-- common HttpConfiguration defined in jetty.xml               -->
  41.   <!-- Add a SecureRequestCustomizer to extract certificate and    -->
  42.   <!-- session information                                         -->
  43.   <!-- =========================================================== -->
  44.   <New id="sslHttpConfig" class="org.eclipse.jetty.server.HttpConfiguration">
  45.     <Arg><Ref refid="httpConfig"/></Arg>
  46.     <Call name="addCustomizer">
  47.       <Arg><New class="org.eclipse.jetty.server.SecureRequestCustomizer"/></Arg>
  48.     </Call>
  49.   </New>
  50. </Configure>
复制代码
编辑Jetty服务器目录中的etc中的jetty-https.xml文件,配置https所使用的443端口。
  1. <?xml version="1.0"?>
  2. <!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
  3. <!-- ============================================================= -->
  4. <!-- Configure a HTTPS connector.                                  -->
  5. <!-- This configuration must be used in conjunction with jetty.xml -->
  6. <!-- and jetty-ssl.xml.                                            -->
  7. <!-- ============================================================= -->
  8. <Configure id="Server" class="org.eclipse.jetty.server.Server">
  9.   <!-- =========================================================== -->
  10.   <!-- Add a HTTPS Connector.                                      -->
  11.   <!-- Configure an o.e.j.server.ServerConnector with connection   -->
  12.   <!-- factories for TLS (aka SSL) and HTTP to provide HTTPS.      -->
  13.   <!-- All accepted TLS connections are wired to a HTTP connection.-->
  14.   <!--                                                             -->
  15.   <!-- Consult the javadoc of o.e.j.server.ServerConnector,        -->
  16.   <!-- o.e.j.server.SslConnectionFactory and                       -->
  17.   <!-- o.e.j.server.HttpConnectionFactory for all configuration    -->
  18.   <!-- that may be set here.                                       -->
  19.   <!-- =========================================================== -->
  20.   <Call id="httpsConnector" name="addConnector">
  21.     <Arg>
  22.       <New class="org.eclipse.jetty.server.ServerConnector">
  23.         <Arg name="server"><Ref refid="Server" /></Arg>
  24.         <Arg name="acceptors" type="int"><Property name="ssl.acceptors" default="-1"/></Arg>
  25.         <Arg name="selectors" type="int"><Property name="ssl.selectors" default="-1"/></Arg>
  26.         <Arg name="factories">
  27.           <Array type="org.eclipse.jetty.server.ConnectionFactory">
  28.             <Item>
  29.               <New class="org.eclipse.jetty.server.SslConnectionFactory">
  30.                 <Arg name="next">http/1.1</Arg>
  31.                 <Arg name="sslContextFactory"><Ref refid="sslContextFactory"/></Arg>
  32.               </New>
  33.             </Item>
  34.             <Item>
  35.               <New class="org.eclipse.jetty.server.HttpConnectionFactory">
  36.                 <Arg name="config"><Ref refid="sslHttpConfig"/></Arg>
  37.               </New>
  38.             </Item>
  39.           </Array>
  40.         </Arg>
  41.         <Set name="host"><Property name="jetty.host" /></Set>
  42.         <Set name="port"><Property name="https.port" default="443" /></Set>
  43.         <Set name="idleTimeout"><Property name="https.timeout" default="30000"/></Set>
  44.         <Set name="soLingerTime"><Property name="https.soLingerTime" default="-1"/></Set>
  45.         <Set name="acceptorPriorityDelta"><Property name="ssl.acceptorPriorityDelta" default="0"/></Set>
  46.         <Set name="selectorPriorityDelta"><Property name="ssl.selectorPriorityDelta" default="0"/></Set>
  47.         <Set name="acceptQueueSize"><Property name="https.acceptQueueSize" default="0"/></Set>
  48.       </New>
  49.     </Arg>
  50.   </Call>
  51. </Configure>
复制代码
编辑Jetty服务器目录中的start.ini文件,按需求更改端口号,并设置启动加载jetty-https.xml,jetty-ssl.xml。
  1. jetty.port=80
  2. jetty.dump.stop=
  3. etc/jetty-ssl.xml
  4. etc/jetty-https.xml
复制代码
重启Jetty,验证https访问是否正常。
腾讯云
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

精彩图文



在线客服(工作时间:9:00-22:00)
400-600-6565

内容导航

微信客服

Copyright   ©2015-2019  云服务器社区  Powered by©Discuz!  技术支持:尊托网络     ( 湘ICP备15009499号-1 )