Tomcat8.5和Tomcat9安装SSL证书的教程

发布时间:2022/9/23 9:59:40 打印 字号:

第一步:准备好所需要的证书文件,Tomcat8.5和Tomcat9部署ssl证书一般使用JKS文件,如果有Pem和key文件,也需要合成JKS文件。

第二步:打开tomcat配置文件 conf/server.xml,tomcat默认一般是8080端口或者 80端口,先找到这一段。

 
<Connector port="8080" protocol="HTTP/1.1"
    connectionTimeout="20000"
    redirectPort="8443" />

第三步:在这段下面插入下面配置:
 
<Connector port="443"
    protocol="org.apache.coyote.http11.Http11Nio2Protocol"
    maxThreads="150"
    SSLEnabled="true"
    defaultSSLHostConfigName="tongpeifu.cn">
    <SSLHostConfig hostName="tongpeifu.cn">
        <Certificate certificateKeystoreFile="conf/ssl/tongpeifu.cn.jks"
            certificateKeystorePassword="123456"
            type="RSA" />
    </SSLHostConfig>
</Connector>

其中代码中的443端口是默认的Https端口,也可以自定义端口, tongpeifu.jks 替换为您自己的证书文件,密码替换为您自己设定的密码。

第四步:重启Tomcat服务即可成功配置SSL证书。

如果需要在Tomcat8.5和Tomcat9版本设置强制https跳转,方法如下:
 
打开tomcat/conf/web.xml中的</welcome-file-list>后面增加以下内容,实现强制跳转:
    <login-config>
     <!-- Authorization setting for SSL -->
     <auth-method>CLIENT-CERT</auth-method>
     <realm-name>Client Cert Users-only Area</realm-name>
     </login-config>
     <security-constraint>
     <!-- Authorization setting for SSL -->
     <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>
 
重启tomcat后即可使用https访问。访问http://域名:8080也会跳转到https://域名。