Tomcat 啟用 HTTPS
最近在阿里雲購買了一個雲服務器ECS,簡單的部署了個人網站,使用ip地址訪問一切正常,我之前在TK上申請了一個免費域名,就綁定了二級域名到阿里雲,第一天正常訪問,第二天就不行了,訪問站點直接提示域名未注冊(未備案.....),無法訪問,顯然被阿里雲攔截了,通過IP地址訪問還是正常的。
后來GOOGLE了一下,一般是備案域名和主機。。。,還有一種方式是啟用HTTPS,http是明文傳輸,阿里雲能夠看到我訪問的域名,發現在域名未注冊然后攔截,但HTTPS是加密的,所以可以正常訪問(哈哈)。
引用鏈接:
https://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html
http://www.oschina.net/question/12_23148
http://www.cnblogs.com/f1194361820/p/4748590.html
基本步驟:
- 使用 java 創建一個 keystore 文件
- 配置 Tomcat 以使用該 keystore 文件
- 測試
- 配置應用以便使用 SSL ,例如 https://localhost:8443/yourApp
執行 keytool -genkey -alias tomcat -keyalg RSA 結果如下
loiane:bin loiane$ keytool -genkey -alias tomcat -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 code for this unit? [Unknown]: BR Is 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): password Re-enter new password: password
這樣就在用戶的主目錄下創建了一個 .keystore 文件
配置 Tomcat 以使用 keystore 文件
打開 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="${user.home}/.keystore" keystorePass="password" protocol="org.apache.coyote.http11.Http11NioProtocol" scheme="https" secure="true" sslProtocol="TLS" />
測試1:只啟用HTTPS,不啟用HTTP
在server.xml中,為8080端口的Connector加上注釋,並且去掉8443端口的Connector的注釋。然后啟動tomcat。
分別通過http\https訪問docs:
http://localhost:8080/docs、https://localhost:8443/docs
結果:只有https可以訪問。
測試2:HTTP、HTTPS同時啟用
在server.xml中,去掉8080、8443端口的Connector的注釋然后啟動tomcat。
分別用http\https訪問docs:
http://localhost:8080/docs、https://localhost:8443/docs
結果:兩者都可正常訪問。
配置應用使用 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 即可
SSL證書服務
文章到這里基本上就應該結束了,可是,當我通過https訪問網站時問題提示“There is a problem with this website’s security certificate.”,自己還好說無所謂,但是別人訪問的時候也提示,可能很大程度上,別人可能就不會再訪問了。。。。,
所以需要SSL證書服務,網上也有很多免費的(https://www.zhihu.com/question/19578422)
我選擇的是https://buy.wosign.com/free/
Create a local Certificate Signing Request (CSR)
使用你之前創建的.keystore
keytool -certreq -keyalg RSA -alias tomcat -file certreq.csr -keystore <your_keystore_filename>
一般順利的話最終你可以拿到一些*.crt文件,
接下來就是把這些文件導入到.keystore文件里。
比如我拿到的是四個壓縮包
for Apache.zip
for IIS.zip
for Nginx.zip
for Other Server.zip
然后我使用的是for Other Server,里面有四個.crt
1_cross_Intermediate.crt
2_issuer_Intermediate.crt
3_user_contacts09.ml.crt
root.crt
先執行root,1_cross_Intermediate.crt,2_issuer_Intermediate.crt
keytool -import -alias <root> -keystore <your_keystore_filename> -trustcacerts -file <filename_of_the_chain_certificate>
最后執行 3_user_contacts09.ml.crt
keytool -import -alias tomcat -keystore <your_keystore_filename> -file <your_certificate_filename>
最后得到的.keystore直接替換之前的就可以了,現在再次訪問就不會提示你證書問題了,哈哈。。。。。