1.生成证书
(1)在jdk的安装目录\bin\keytool.exe下打开keytool.exe
![](https://i-blog.csdnimg.cn/blog_migrate/f73eb3174ca9ed9cea775f470b5a4113.png)
在命令行中输入以下命令:
keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keystore "E:\tomcat.keystore" -validity 36500
![](https://i-blog.csdnimg.cn/blog_migrate/58802e596fc429b623d5e1b96c0954b9.png)
以上命令将生产一对非对称密钥和自我签名的证书E:\tomcat.keystore
注意:“名字与姓氏”应该是域名,输成了姓名,和真正运行的时候域名不符,会出问题
这里我输入的密码是123456, 域名是以tomcat为例, 省市以广东深圳为例
2. 配置tomcat服务器
定位到tomcat服务器的安装目录, 找到conf下的server.xml文件
![](https://i-blog.csdnimg.cn/blog_migrate/8a09745a8a96458c7b85fca820268c22.png)
找到如下已经被注释的代码:
![](https://i-blog.csdnimg.cn/blog_migrate/aea68fed898cc8e3e5a376d7a013331c.png)
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" />
-->
去掉注释,修改为:
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="E:\tomcat.keystore"
keystorePass="123456" />
![](https://i-blog.csdnimg.cn/blog_migrate/a8a7aabeca2a10946c3a4f769d17decc.png)
在互联网上, http协议的默认端口是80, https的默认端口是443, 这里将端口改为了443
3.HTTP自动跳转配置
配置Tomcat,打开$CATALINA_HOME/conf/server.xml,修改如下
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
修改为
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443" />
<!--
<Connector port="8009" enableLookups="false" protocol="AJP/1.3" redirectPort="8443" />
-->
修改为:
<Connector port="8009" enableLookups="false" protocol="AJP/1.3" redirectPort="443" />
在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>
![](https://i-blog.csdnimg.cn/blog_migrate/2707508d8159864833e138e4b47eacc2.png)