SSL簡單介紹
SSL(Secure Sockets Layer 安全套接層)就是一種協議(規范),用于保障客戶端和服務器端通信的安全,以免通信時傳輸的信息被竊取或者修改。
1.怎樣保障數據傳輸安全?
客戶端和服務器端在進行握手(客戶端和服務器建立連接和交換參數的過程稱之為握手)時會產生一個“對話密鑰”(session key),用來加密接下來的數據傳輸,解密時也是用的這個“對話密鑰”,而這個“對話密鑰”只有客戶端和服務器端知道。也就是說只要這個“對話密鑰”不被破解,就能保證安全。
2. 客戶端證書和服務器端證書
客戶端證書和服務器端證書用于證明自己的身份,就好比每個人都有一張身份證,這種身份證是唯一的。一般來說,只要有服務器端的證書就可以了,但是有時需要客戶端提供自己的證書,已證明其身份。
Keytool
Keytool 是一個Java數據證書的管理工具 ,Keytool將密鑰(key)和證書(certificates)存在一個稱為keystore的文件中在keystore里,包含兩種數據:密鑰實體(Key entity)-密鑰(secret key)或者是私鑰和配對公鑰(采用非對稱加密)可信任的證書實體(trusted certificate entries)-只包含公鑰。下面就來看看利用keytools為tomcat 7配置ssl雙向認證的詳細過程吧。
第一、證書庫、證書等生成
1、生成服務器證書庫
1
|
keytool -validity 36500 -genkey -v -alias tomcat_server -keyalg RSA -keystore tomcat_server.keystore -dname "CN=127.0.0.1,OU=,O=,L=,ST=,c=" -storepass 123456 -keypass 123456 |
- -validity 36500 有效期,以天為單位
- CN 這項一定是服務器的域名或者IP地址
- OU 組織單位
- O 組織
- L 區域
- ST 州/省份
- C 國家
2、客戶端證書
1
|
keytool -validity 36500 -genkeypair -v -alias testclient -keyalg RSA -storetype PKCS12 -keystore testclient.p12 -dname "CN=testclient,OU=,O=,L=,ST=,c=" -storepass 123456 |
-storetype PKCS12 主要是為了將證書導入IE/firefox 中。
將生成的證書導入IE中。
3、將客戶端證書導入服務器端證書庫
服務器端證書不識別 p12格式的證書,需要從客戶端證書導出 CER格式證書,然后將CER格式證書導入到服務器端證書中。
1
|
keytool -export -alias testclient -keystore testclient.p12 -storetype PKCS12 -storepass 123456 -rfc -file testclient.cer |
然后將client.cer 導入到服務器證書庫(使用下面任意一個命令)
1
2
|
keytool - import -v -file testclient.cer -keystore tomcat_server.keystore -storepass 123456 keytool - import -alias testclient -v -file testclient.cer -keystore tomcat_server.keystore -storepass 123456 |
注意:這里的別名,如果不加別名,則默認別名則是 mykey,所以見到mykey 請不要吃驚。
4、從服務器證書庫導出服務器證書
1
|
keytool -export -alias tomcat_server -keystore tomcat_server.keystore -storepass 123456 -rfc -file tomcat_server.cer |
該證書可以導入瀏覽器中,讓客戶端信任服務器證書。不導入也不影響使用,但瀏覽器會不信任服務器證書,會提示錯誤信息。
5、查看證書庫中的所有證書
1
|
keytool -list -keystore tomcat_server.keystore -storepass 123456 |
第二、Tomcat 配置
配置 server.xml
1
2
3
4
5
6
7
|
< Connector port = "8443" protocol = "HTTP/1.1" SSLEnabled = "true" maxThreads = "150" scheme = "https" secure = "true" keystoreFile = "D:\\dev\\tomcat-https\\note\\tomcat_server.keystore" keystorePass = "123456" truststoreFile = "D:\\dev\\tomcat-https\\note\\tomcat_server.keystore" truststorePass = "123456" clientAuth = "true" sslProtocol = "TLS" /> |
啟動tomcat 就可以了。
問題
如果啟動時報如下錯誤:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
SEVERE: Failed to initialize end point associated with ProtocolHandler ["http-apr-8443"] java.lang.Exception: Connector attribute SSLCertificateFile must be defined when using SSL with APR at org.apache.tomcat.util.net.AprEndpoint.bind(AprEndpoint.java:507) at org.apache.tomcat.util.net.AbstractEndpoint.init(AbstractEndpoint.java:610) at org.apache.coyote.AbstractProtocol.init(AbstractProtocol.java:429) at org.apache.catalina.connector.Connector.initInternal(Connector.java:981) at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102) at org.apache.catalina.core.StandardService.initInternal(StandardService.java:559) at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102) at org.apache.catalina.core.StandardServer.initInternal(StandardServer.java:814) at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102) at org.apache.catalina.startup.Catalina.load(Catalina.java:640) at org.apache.catalina.startup.Catalina.load(Catalina.java:665) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.apache.catalina.startup.Bootstrap.load(Bootstrap.java:281) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:455) |
則是由于Tomcat中的SSL采用了 APR來實現的,關于SSL的實現,Tomcat提供了兩種:JSSE和APR,如果安裝了 APR,則優先選擇APR作為實現。
APR的ssh配置需要通OpenSSH來進行配置。這在 server.xml 中有說明:
1
2
|
Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the JSSE configuration, when using APR, the connector should be using the OpenSSL style configuration described in the APR documentation |
那么避免采用 APR呢? 有兩種方法,
1,將 protocol=”HTTP/1.1” 修改為 protocol=”org.apache.coyote.http11.Http11Protocol”
2,在windows 下,可以將 bin 目錄下的 tcnative-1.dll 刪掉。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流。