一、爲什麼要配置SSL證書
HTTPS
能有效保護用戶隱私,能進行有效的身份校驗,並能保證數據的保密性、完整性。- 交流數據加密,爲服務器端與客戶端之間的數據傳輸進行加密。例如微信小程序要求被提供接口的一方要安裝
SSL
證書。 - 可進行
SEO
優化,百度官方公開說明搜索引擎優先HTTPS
網站。 - 避免瀏覽器彈出
不安全
警告,添加SSL
證書增強網站可信度。
二、所有免費的 SSL
證書
SSL 證書分爲三種類型:域名型SSL證書(DV SSL)、企業型SSL證書(OVSSL)、增強型SSL證書(EVSSL)。
本篇主要介紹下面三個平臺,因爲大家購買的服務大多是騰訊雲或者阿里雲的。
1.阿里雲DV SSL證書
官網:https://common-buy.aliyun.com/?commodityCode=cas
阿里雲的證書是有 Symantec 簽發,Symantec 公司是 SSL/TLS 證書的領先提供商,爲全球一百多萬臺網絡服務器提供安全防護。一個阿雲帳戶最多簽發20張免費證書。
2.騰訊雲DV SSL 證書
官網:https://cloud.tencent.com/product/ssl
騰訊雲DV SSL 域名型證書也是由 Symantec 公司簽發。快速便捷部署到騰訊雲,也可以下載證書,用於其他雲服務提供商。
3.Let’s Encrypt
官網: https://letsencrypt.org
Let’s Encrypt是國外一個公共的免費SSL項目,由 Linux 基金會託管,安裝部署特別簡單,總之得到大家的一致認可。
三、Tomcat 安裝阿里雲 SSL
證書
申請證書
證書申請地址:https://common-buy.aliyun.com/?commodityCode=cas
證書類型選擇:免費版(個人)DV,如下圖:
立即購買後,進入到 ssl
證書控制檯,選中一個證書,點擊 "證書申請"按鈕,如下圖:
填寫相應的必填信息,按照提示進入操作。
下載證書
上面申請24小時內會通過,接下來根據自己的 web
容器下載相應的證書,這裏我使用的容器爲 Tomcat
,如下圖:
下載下來的證書包含兩個文件:
Tomcat 配置
1.上傳證書文件
上傳證書到服務器上,這裏目錄根據自己可自由設置,我上傳的路徑爲 tomcat/cert
。
2.修改 conf/server.xml
a.修改 tomcat
訪問端口,將 8080 改爲 80,在瀏覽器訪問時不需要添加端口。將 redirectPort="8443"
的端口改爲 443,因爲 https 的端口爲 443。最終修改內容如下:
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<Connector port="80" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="443" />
b.添加 ssl
證書和密碼,將 port
值改爲 443,keystoreFile
屬性指向服務器的 *.pfx文件,keystoreType
的值爲 pfx-password.txt
的內容,最終修改內容如下:
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443
This connector uses the NIO implementation. The default
SSLImplementation will depend on the presence of the APR/native
library and the useOpenSSL attribute of the
AprLifecycleListener.
Either JSSE or OpenSSL style configuration may be used regardless of
the SSLImplementation selected. JSSE style configuration is used below.
-->
<Connector port="443"
protocol="org.apache.coyote.http11.Http11Protocol"
SSLEnabled="true"
scheme="https"
secure="true"
keystoreFile="/xxx/tomcat/cert/restlessman.cn.pfx"
keystoreType="PKCS12"
keystorePass="xxxxx"
clientAuth="false"
SSLProtocol="TLSv1+TLSv1.1+TLSv1.2"
ciphers="TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_256_CBC_SHA256"/>
3.修改 conf/web.xml
在 conf/web.xml
添加如下內容,可從 http 跳轉到 https
<login-config>
<!-- Authorization setting for SSL -->
<auth-method>CLIENT-CERT</auth-method>
<realm-name>Client Cert Users-only Area</realm-name>
</login-config>
<security-constraint>
<!-- Authorization setting for SSL -->
<web-resource-collection >
<web-resource-name >SSL</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
4.重啓 Tomcat
重啓 Tomcat
後,訪問 https://xxx.xxx.xxx 即可。如下圖:
四、最後
之前一直比較懶,搭建很多博客網站一直沒安裝 ssl
證書,對於博客而言安全證書意義不大!這次只因一件事,小程序request合法域名必須是 https
開頭,所以就出現了本篇文章。有講的不對的地方歡迎指正!