配置Tomcat使用https协议
1 在jdk的安装目录\bin\keytool.exe下打开命令行
2 在命令行中输入以下命令:
keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keystore "d:\tomcat.keystore"
3 配置tomcat服务器配置文件server.xml
找到如下已经被注释的代码:
去掉注释,修改为:
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS"
keystoreFile="d:\tomcat.keystore"
keystorePass="123456" />
提示:
在互联网上, http协议的默认端口是80, https的默认端口是443, 这里将端口改为了443
强制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>
提示:
完成以上步骤后,在浏览器中输入http的访问地址也会自动转换为https了。需要注意的是在server.xml中http的配置中重定向属性的端口号需要设置为https对应的端口号,如443.