1,首先打开cmd命令提示符运行以下命令,keytool -genkey -alias tomcat -keyalg RSA -keypass changeit -storepass changeit -keystore server.keystore -validity 3600
其中:
keystoreFile为“myselfstore”文件所在的路径。
keystorePass为创建“myselfstore”的密码。
运行命令之后会在C:\Users\username目录下生成一个server.keystore的文件,将文件拷贝到tomcat目录下的conf文件夹下面,打开server.xml并找到https的相关配置
<!-- <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" keystoreFile="conf\server.keystore" keystorePass="changeit" sslProtocol="TLS" />
然后重启tomcat
2.添加强制https访问代码,找到tomcat目录下的conf文件夹,打开web.xml文件,添加以下代码
<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访问了 https://localhost:8443/