1、从Let’s Encrypt获取免费证书
可以从该网站申请获取到免费的秘钥和证书信息,详细的操作步骤这里不介绍,可以参考博文:(http://www.laozuo.org/7676.html),该步骤执行完成后,你应该就有了如下几个文件:
2、将密钥转化为Keystore文件
1、将上述生成的四个文件复制到内网服务器的任意路径,然后执行如下命令:执行完该命令后,会提示输入密码,我这里随意设置为123456
openssl pkcs12 -export -in fullchain.pem -inkey privkey.pem -out pkcs.p12 -name NAME
2、继续执行如下命令:(这里要使用到密码的地方我都统一设置为123456),执行完此命令后,会在当前路径上生成keystore.jks文件,也就是jetty实现https需要使用到的文件。
keytool -importkeystore -deststorepass PASSWORD_STORE -destkeypass PASSWORD_KEYPASS -destkeystore keystore.jks -srckeystore pkcs.p12 -srcstoretype PKCS12 -srcstorepass STORE_PASS -alias NAME
3、为了方便,将keystore.jks重命名为jetty.jks,则此时该路径下文件:
3、jetty配置
1、进入jetty服务器目录下的etc,新建存放jks格式证书的目录,并复制jks格式证书至当前目录。
2、编辑jetty服务器目录下的etc下的jetty-ssl.xml,设置证书相关参数,主要是配置jetty.jks的路径和密码等信息。
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- ============================================================= -->
<!-- Configure a TLS (SSL) Context Factory -->
<!-- This configuration must be used in conjunction with jetty.xml -->
<!-- and either jetty-https.xml or jetty-spdy.xml (but not both) -->
<!-- ============================================================= -->
<Configure id="sslContextFactory" class="org.eclipse.jetty.util.ssl.SslContextFactory">
<Set name="KeyStorePath"><Property name="jetty.base" default="." />/<Property name="jetty.keystore" default="etc/cert/jetty.jks"/></Set>
<Set name="KeyStorePassword"><Property name="jetty.keystore.password" default="123456"/></Set>
<Set name="KeyManagerPassword"><Property name="jetty.keymanager.password" default="123456"/></Set>
<Set name="TrustStorePath"><Property name="jetty.base" default="." />/<Property name="jetty.truststore" default="etc/cert/jetty.jks"/></Set>
<Set name="TrustStorePassword"><Property name="jetty.truststore.password" default="123456"/></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>
<!-- =========================================================== -->
<!-- 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>
</Configure>
3、编辑jetty服务器目录下的etc下的jetty-https.xml,配置https所使用的443端口。
<?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="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Add a HTTPS Connector. -->
<!-- Configure an o.e.j.server.ServerConnector with connection -->
<!-- factories for TLS (aka SSL) and HTTP to provide HTTPS. -->
<!-- All accepted TLS connections are wired to a HTTP connection.-->
<!-- -->
<!-- Consult the javadoc of o.e.j.server.ServerConnector, -->
<!-- o.e.j.server.SslConnectionFactory and -->
<!-- o.e.j.server.HttpConnectionFactory for all configuration -->
<!-- that may be set here. -->
<!-- =========================================================== -->
<Call id="httpsConnector" name="addConnector">
<Arg>
<New 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">
<Item>
<New class="org.eclipse.jetty.server.SslConnectionFactory">
<Arg name="next">http/1.1</Arg>
<Arg name="sslContextFactory"><Ref refid="sslContextFactory"/></Arg>
</New>
</Item>
<Item>
<New class="org.eclipse.jetty.server.HttpConnectionFactory">
<Arg name="config"><Ref refid="sslHttpConfig"/></Arg>
</New>
</Item>
</Array>
</Arg>
<Set name="host"><Property name="jetty.host" /></Set>
<Set name="port"><Property name="https.port" default="443" /></Set>
<Set name="idleTimeout"><Property name="https.timeout" default="30000"/></Set>
<Set name="soLingerTime"><Property name="https.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="https.acceptQueueSize" default="0"/></Set>
</New>
</Arg>
</Call>
</Configure>
4、编辑jetty服务器目录下的start.ini文件,按需求更改端口号,并设置启动加载jetty-https.xml,jetty-ssl.xml。
5、设置完成后,然后启动jetty。因为我jetty的http端口设置的为8087,而https端口为443,故该jetty就会启动此两个接口,分别提供http和https服务。
4、测试
该jetty服务安装在内网的192.168.10.102服务器上,http端口为8087,https端口为443
1、使用http访问/api/dc/test接口:
2、使用https访问api/dc/test接口:这个时候,你会发现浏览器 提示的是不安全的。那么是什么原因呢?请看第三步
3、提示不是秘密连接,自然是https没有生效,原因就是我们从Let’s Encrypt获取免费证书的时候,不是会映射一个域名吗?映射完域名后,就表示证书只会对该域名生效,我测试的域名是:www.realxxxxxx.club,则修改本地的host文件,将该域名映射到内网192.168.10.102服务器。然后重新访问,久违的绿锁出现了。