这tomcat配置ssl,让我们的网页以https协议访问
主要的步骤如下
- Create a keystore file using Java
- Configure Tomcat to use the keystore
- Test it
1 – Creating a Keystore file using Java
进入java的安装目录下的bin目录下
cd
%JAVA_HOME%/bin
在bin目录下面有个一个keytool.exe文件,这个文件就是用来生成.keystore文件的
keytool -genkey -
alias
tomcat -keyalg RSA
接着按照提示信息一步步完成。
如果上面的步骤都正确的那么会在C:Documents and Settings[username] 下面生成
一个.keystore的隐藏文件
2 – Configuring Tomcat for using the keystore file – SSL config
到tomcat目录下面的conf目录打开server.xml
找到
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
替换为
Connector SSLEnabled="true" acceptCount="100" clientAuth="false"
disableUploadTimeout="true" enableLookups="false" maxThreads="25"
port="8443" keystoreFile="/Users/loiane/.keystore" keystorePass="password"
protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https"
secure="true" sslProtocol="TLS" />
3 – test it!
随便新建一个web项目部署到tomcat的目录
在浏览器地址栏 输入
https://localhost:8443/yourApp
就可以看到一个红色的警告框,那说明你配置成功了。
如果你想强制你的web应用用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 pattern 设置成* ,那么所有的请求都通过https的方式访问。
transport-guarantee 设置成 confidential 确保你的应该可以在SSL下正常工作
如果你不想用https方式来访问你的网站,那么可以将 transport-guarantee 的值设置成
NONE,那么就不会用https的方式来访问了。
参考
http://java.dzone.com/articles/setting-ssl-tomcat-5-minutes