keytool -genkey -alias tomcat -keyalg RSA -validity 3600 -keystore .keystore
在/tomcat/conf的安装目录下的找到server.xml 文件,修改 server.xml 文件,配置https连接器。新增一个Connector,tomcatm默认只有8080的。
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true"> <SSLHostConfig> <Certificate certificateKeystoreFile="conf/.keystore" type="RSA" certificateKeystorePassword="123456" /> </SSLHostConfig> </Connector>
在项目的web.xml最后加上如下代码即可
<!-- 配置http自动转为 https --> <security-constraint> <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>