本文转帖自红薯的帖子:http://www.oschina.net/question/12_23148?sort=default&p=2#answers html
为方便本身查询故保存原文内容。 java
本教程使用 JDK 6 和 Tomcat 7,其余版本相似。 web
基本步骤: apache
1. 建立 keystore 文件 tomcat
执行 keytool -genkey -alias tomcat -keyalg RSA 结果以下 app
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
loiane:bin loiane$ keytool -genkey -aliastomcat -keyalg RSA
Enter keystore password: password
Re-enter new password: password
What is your first and last name?
[Unknown]: Loiane Groner
What is the name of your organizational unit?
[Unknown]: home
What is the name of your organization?
[Unknown]: home
What is the name of your City or Locality?
[Unknown]: Sao Paulo
What is the name of your State or Province?
[Unknown]: SP
What is the two-letter country codeforthis unit?
[Unknown]: BR
Is CN=Loiane Groner, OU=home, O=home, L=Sao Paulo, ST=SP, C=BR correct?
[no]: yes
Enter key passwordfor
(RETURNifsame as keystore password): password
Re-enter new password: password
|
这样就在用户的主目录下建立了一个 .keystore 文件 测试
2. 配置 Tomcat 以使用 keystore 文件 this
打开 server.xml 找到下面被注释的这段 url
1
2
3
4
5
|
<!--
<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
maxThreads="150" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
|
干掉注释,并将内容改成 spa
1
2
3
4
5
|
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. 测试
启动 Tomcat 并访问 https://localhost:8443. 你将看到 Tomcat 默认的首页。
须要注意的是,若是你访问默认的 8080 端口,仍是有效的。
4. 配置应用使用 SSL
打开应用的 web.xml 文件,增长配置以下:
1
2
3
4
5
6
7
8
9
|
<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 便可。
官方文档: http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html