Using JDK 7 and Tomcat 7, similar to other versions.
Step:
1. 用Java去创建一个keystore文件,执行 keytool genkey - alias tomcat - keyalg RSA.
因此在用户主目录,创建了一个keystore文件。
C:\Users\calvin.yang>keytool -genkey -alias tomcat -keyalg RSA Enter keystore password: password Re-enter new password: password What is your first and last name? [Unknown]: calvin yang What is the name of your organizational unit? [Unknown]: abc What is the name of your organization? [Unknown]: abc What is the name of your City or Locality? [Unknown]: Zhuhai What is the name of your State or Province? [Unknown]: GuangDong What is the two-letter country code for this unit? [Unknown]: CN Is CN=calvin yang, OU=abc, O=abc, L=Zhuhai, ST=GuangDong, C=CN correct? [no]: yes Enter key password for <tomcat> (RETURN if same as keystore password): password Re-enter new password: password
2. 配置Tomcat,使用这个keystore文件打开server.xml,找到下面的注解
去掉注释,改变成下面的内容<!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> -->
3. 测试<Connector SSLEnabled="true" acceptCount="100" clientAuth="false" disableUploadTimeout="true" enableLookups="false" maxThreads="25" port="8443" keystoreFile="C:/Users/calvin.yang/.keystore" keystorePass="password" protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https" secure="true" sslProtocol="TLS" />
开启Tomcat,访问https://localhost:8443. 你就会看见Tomcat的默认主页面了。
4.使用SSL配置应用程序, 例如:https://localhost:8443/myApp
打开web.xml,加上下面的配置:
设置成 /* URL映射,你的程序的所有访问都会使用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>
设置transport-gurantee为CONFIDENTIAL,为了让程序支持SSL。如果你想要关掉SSL,你只要把CONFIDENTIAL变为NONE。
-------------------------------------------------------------------------------------------------
官方文档:http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html