1.生成证书
(1)在jdk的安装目录inkeytool.exe下打开keytool.exe
在命令行中输入以下命令:
keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keystore "E: omcat.keystore" -validity 36500
以上命令将生产一对非对称密钥和自我签名的证书E: omcat.keystore
注意:“名字与姓氏”应该是域名,输成了姓名,和真正运行的时候域名不符,会出问题
这里我输入的密码是123456, 域名是以tomcat为例, 省市以广东深圳为例
2. 配置tomcat服务器
定位到tomcat服务器的安装目录, 找到conf下的server.xml文件
找到如下已经被注释的代码:
<!-- <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: omcat.keystore" keystorePass="123456" />
在互联网上, 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" />
在tomcatconfweb.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>