原文:http://www.oschina.net/question/12_23148
1、生成证书
利用java提供的keytool工具生成证书,工具路径:C:\Program Files (x86)\Java\jre1.8.0_131\bin\keytool.exe
cmd控制台到该路径下,输入命令 keytool -genkey -alias tomcat -keyalg RSA
生成证书在用户目录下
2、配置tomcat使用keystore文件
修改tomcat的conf\server.xml文件
<!-- <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" /> -->
<Connector SSLEnabled="true" acceptCount="100" clientAuth="false"
disableUploadTimeout="true" enableLookups="false" maxThreads="25"
port="8443" keystoreFile="C:/Users/liu/.keystore" keystorePass="liuzhenpeng"
protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https"
secure="true" sslProtocol="TLS" />
去掉注释,修改成如上设置
执行完上述设置后,应用可以通过http或者https进行访问
3、配置应用使用SSL
在web.xml添加如下配置
<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>
将 URL 映射设为 /* ,这样你的整个应用都要求是 HTTPS 访问,而 transport-guarantee 标签设置为 CONFIDENTIAL 以便使应用支持 SSL。
如果你希望关闭 SSL ,只需要将 CONFIDENTIAL 改为 NONE 即可。