天天看點

linux下tomcat8.5.4配置ssl

tomcat不同的版本對ssl的配置有所不同。8.0以下的版本網上很多,這裡就不去說了。下面主要記錄的就是8.5.4對ssl的配置

1.擷取ssl。

我這裡使用的是阿裡雲提供的ssl。操作步驟:

①:進入阿裡雲的域名管理

②:選擇ssl證書,如下圖

linux下tomcat8.5.4配置ssl

③:如果已經申請了ssl證書,就直接點選證書控制台。沒有的可以通過購買或者申請免費證書。

linux下tomcat8.5.4配置ssl

④:選擇需要的證書下載下傳

linux下tomcat8.5.4配置ssl
linux下tomcat8.5.4配置ssl

2.配置證書

在上面的下載下傳證書的頁面有關于證書的配置,但是這個配置不适用于8.5.4,但是配置前面的步驟是通用的。我配置的是JKS證書

linux下tomcat8.5.4配置ssl

生成jks證書之後通過指令檢視證書:keytool -keystore your-name.jks -list

顯示如下

linux下tomcat8.5.4配置ssl

3:配置tomcat中的server.xml檔案

這是tomcat自帶的配置例子;

linux下tomcat8.5.4配置ssl

修改為:

linux下tomcat8.5.4配置ssl
<Connector port="端口" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
            <Certificate certificateKeystoreFile="jks檔案位置"
						certificateKeystorePassword="上面步驟的密碼"
						certificateKeyAlias="上面查詢出來的别名"
                         type="RSA" />
        </SSLHostConfig>
    </Connector>
           

<Connector port="端口" protocol="org.apache.coyote.http11.Http11NioProtocol"

               maxThreads="150" SSLEnabled="true">

        <SSLHostConfig>

            <Certificate certificateKeystoreFile="jks檔案位置"

                        certificateKeystorePassword="上面步驟的密碼"

                        certificateKeyAlias="上面查詢出來的别名"

                         type="RSA" />

        </SSLHostConfig>

    </Connector>

4:将redirectPort的端口配置到ssl的端口上。

5:将http的請求自動跳轉到https上。

在web.xml的末尾加上代碼

<security-constraint>

    <web-resource-collection >

              <web-resource-name >SSL</web-resource-name>

              <url-pattern>/*</url-pattern>

       </web-resource-collection>

       <user-data-constraint>

       <transport-guarantee>CONFIDENTIAL</transport-guarantee>

       </user-data-constraint>

</security-constraint>
           

<security-constraint>     <web-resource-collection >               <web-resource-name >SSL</web-resource-name>               <url-pattern>/*</url-pattern>        </web-resource-collection>        <user-data-constraint>        <transport-guarantee>CONFIDENTIAL</transport-guarantee>        </user-data-constraint> </security-constraint>