本教程使用 JDK 6 和 Tomcat 7,其他版本類(lèi)似。
基本步驟:
1. 創(chuàng)建 keystore 文件
執(zhí)行 keytool -genkey -alias tomcat -keyalg RSA 結(jié)果如下
loiane:bin loiane$ keytool -genkey -alias tomcat -keyalg RSAEnter keystore password: passwordRe-enter new password: passwordWhat is your first and last name? [Unknown]: Loiane GronerWhat is the name of your organizational unit? [Unknown]: homeWhat is the name of your organization? [Unknown]: homeWhat is the name of your City or Locality? [Unknown]: Sao PauloWhat is the name of your State or Province? [Unknown]: SPWhat is the two-letter country code for this unit? [Unknown]: BRIs CN=Loiane Groner, OU=home, O=home, L=Sao Paulo, ST=SP, C=BR correct? [no]: yes Enter key password for (RETURN if same as keystore password): passwordRe-enter new password: password
這樣就在用戶(hù)的主目錄下創(chuàng)建了一個(gè) .keystore 文件
2. 配置 Tomcat 以使用 keystore 文件
打開(kāi) server.xml 找到下面被注釋的這段
<!--<Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" />-->
干掉注釋?zhuān)?nèi)容改為
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. 測(cè)試
啟動(dòng) Tomcat 并訪(fǎng)問(wèn) https://localhost:8443. 你將看到 Tomcat 默認(rèn)的首頁(yè)。
需要注意的是,如果你訪(fǎng)問(wèn)默認(rèn)的 8080 端口,還是有效的。
4. 配置應(yīng)用使用 SSL
打開(kāi)應(yīng)用的 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 映射設(shè)為 /* ,這樣你的整個(gè)應(yīng)用都要求是 HTTPS 訪(fǎng)問(wèn),而 transport-guarantee 標(biāo)簽設(shè)置為 CONFIDENTIAL 以便使應(yīng)用支持 SSL。
如果你希望關(guān)閉 SSL ,只需要將 CONFIDENTIAL 改為 NONE 即可。
官方文檔: http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html
聯(lián)系客服