可以使用%JAVA_HOME%\bin下的keytool来生成keystore,使用如下命令,会生成到~目录下
%JAVA_HOME%\bin\keytool -genkey -alias tomcat -keyalg RSA
去掉tomcat conf下server.xml的一段注释并修改如下,其中password为上一步生成keystore时填写的。
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true" keystorePass="password"
clientAuth="false" sslProtocol="TLS" />
经过之前的步骤,可同时访问http和https,在自己应用的web.xml下加上以下代码或使用注解的方式来到达自动将访问http转https的目的
<security-constraint>
<web-resource-collection>
<web-resource-name>securedapp</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>