一、前言
建立客戶端與服務器的Https的連接需要證書進行雙向驗證后,才可訪問。
二、證書類型

不同數字證書部署在服務器上后,用戶瀏覽器訪問網站時,展示如下:
1、無證書時
顯示不安全標識。

2、域名型(DV)SSL
可以顯示安全連接標識,證書無法顯示組織信息。

3、企業型(OV)SSL
可以顯示安全連接標識,證書詳細內顯示組織信息。

4、增強型(EV)SSL
地址欄綠色,可以顯示安全連接標識,地址欄直接顯示組織信息。

三、生成SSL證書
證書的創建與作用

生成環境:
Linux: CentOS_7(及以上)
Tomcat: Apache Tomcat_8.5.42(及以上)
OpenSSL: OpenSSL 1.1.0c 下載路徑:https://www.openssl.org/source/openssl-1.1.0c.tar.gz
- 制作服務器根證書
1.解壓安裝
#解壓tar包
tar -xzvf openssl-1.1.0c.tar.gz
#進入加壓后的文件夾內
cd openssl-1.1.0c
#安裝openssl到 /usr/local/openssl 目錄,安裝之后,編譯;這個時候執行需要有耐心,可以查看命令行一直在滾屏,等到滾屏結束,安裝編譯完成
./config --prefix=/usr/local/openssl && make && make install
#重命名
mv /usr/bin/openssl /usr/bin/openssl.ori
mv /usr/include/openssl /usr/include/openssl.ori
#將安裝好的openssl命令軟連到對應位置
ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl
ln -s /usr/local/openssl/include/openssl /usr/include/openssl
#在/etc/ld.so.conf文件中寫入openssl庫文件的搜索路徑
echo /usr/local/openssl/lib >> /etc/ld.so.conf
ldconfig -v
openssl version -a
2.生成證書
2.1 創建根證書密鑰文件(自己做CA) root.key
#生成命令
openssl genrsa -des3 -out root.key 2048
#輸出日志
Generating RSA private key, 2048 bit long modulus
.....................................................................................................................+++
..........................+++
e is 65537 (0x010001)
Enter pass phrase for root.key: ← #在這輸入一個新密碼
Verifying – Enter pass phrase for root.key: ← 在這重新輸入一遍密碼
2.2 創建根證書的申請文件 root.csr
#生成命令
openssl req -new -key root.key -out root.csr
#輸出日志
Enter pass phrase for root.key: ← 在這輸入前面創建的密碼
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [AU]:CN ← 國家代號,中國輸入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不輸入
Common Name (eg, YOUR name) []: ← 此時不輸入
Email Address []:admin@mycompany.com ← 電子郵箱,可隨意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不輸入
An optional company name []: ← 可以不輸入
2.3 創建一個自當前日期起為期十年的根證書 root.crt
#生成命令
openssl x509 -req -days 3650 -sha256 -extfile /usr/local/openssl/ssl/openssl.cnf -extensions v3_ca -signkey root.key -in root.csr -out root.crt
#輸出日志
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./emailAddress=admin@mycompany.com
Getting Private key
Enter pass phrase for root.key: ← 在這輸入前面創建的密碼
2.4 根據CA證書生成truststore JKS文件 root.truststore
這一步只針對雙向認證,單向不需要
#生成命令
keytool -keystore root.truststore -keypass 123456 -storepass 123456 -alias ca -import -trustcacerts -file /tmp/ca/root.crt
#鍵入回車后,提示是否信任此證書,輸入yes, 則生成truststore成功
- 制作service服務器端證書
1.創建服務器證書密鑰 server.key
#生成命令
openssl genrsa -des3 -out server.key 2048
#輸出內容為:
Generating RSA private key, 2048 bit long modulus
...........................+++
...............+++
e is 65537 (0x010001)
Enter pass phrase for server.key: ←在這輸入前面創建的密碼
Verifying - Enter pass phrase for server.key: ← 在這重新輸入一遍密碼
#運行時會提示輸入密碼,此密碼用於加密key文件(參數des3便是指加密算法,當然也可以選用其他你認為安全的算法.),以后每當需讀取此文件(通過openssl提供的命令或API)都需輸入口令(密碼).如果覺得不方便,也可以去除這個口令,但一定要采取其他的保護措施!
#去除口令的命令
openssl rsa -in server.key -out server.key
2.創建服務器證書的申請文件 server.csr
#生成命令
openssl req -new -key server.key -out server.csr
#輸出內容為:
Enter pass phrase for server.key: ← 輸入前面創建的密碼
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [AU]:CN ← 國家名稱,中國輸入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省名,拼音
Locality Name (eg, city) []:BeiJing ← 市名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不輸入
Common Name (eg, YOUR name) []:www.xxx.com ← 服務器主機名(或者IP),若填寫不正確,瀏覽器會報告證書無效,但並不影響使用
Email Address []:admin@mycompany.com ← 電子郵箱,可隨便填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不輸入
An optional company name []: ← 可以不輸入
3.創建自當前日期起有效期為期十年的服務器證書 server.crt
#生成命令
openssl x509 -req -days 3650 -sha256 -extfile /usr/local/openssl/ssl/openssl.cnf -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in server.csr -out server.crt
#輸出內容為:
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com
Getting CA Private Key
Enter pass phrase for root.key: ← 輸入前面創建的密碼
4.導出.p12文件 server.p12
#生成命令
openssl pkcs12 -export -in /tmp/ca/server.crt -inkey /tmp/ca/server.key -out /tmp/ca/server.p12 -name "server"
根據命令提示,輸入server.key密碼,創建p12密碼。
5.將.p12 文件導入到keystore JKS文件 server.keystore
#生成命令
keytool -importkeystore -v -srckeystore /tmp/ca/server.p12 -srcstoretype pkcs12 -srcstorepass 123456 -destkeystore /tmp/ca/server.keystore -deststoretype jks -deststorepass 123456
這里srcstorepass后面的123456為server.p12的密碼deststorepass后的123456為keyStore的密碼
- 制作Client客戶端證書
1.創建客戶端證書密鑰文件 client.key
#生成命令
openssl genrsa -des3 -out client.key 2048
#輸出內容為:
Generating RSA private key, 2048 bit long modulus
...............................+++
.........................+++
e is 65537 (0x010001)
Enter pass phrase for client.key: ← 輸入新密碼
Verifying – Enter pass phrase for client.key: ← 重新輸入一遍密碼
2.創建客戶端證書的申請文件 client.csr
#生成命令
openssl req -new -key client.key -out client.csr
#輸出內容為:
Enter pass phrase for client.key: ← 輸入上一步中創建的密碼
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
—–
Country Name (2 letter code) [AU]:CN ← 國家名稱,中國輸入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省名稱,拼音
Locality Name (eg, city) []:BeiJing ← 市名稱,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不填
Common Name (eg, YOUR name) []:Lenin ← 自己的英文名,可以隨便填
Email Address []:admin@mycompany.com ← 電子郵箱,可以隨便填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不填
An optional company name []: ← 可以不填
3.創建一個自當前日期起有效期為十年的客戶端證書 client.crt
#生成命令
openssl x509 -req -days 3650 -sha256 -extfile /usr/local/openssl/ssl/openssl.cnf -extensions v3_req -CA root.crt -CAkey root.key -CAcreateserial -in client.csr -out client.crt
#輸出內容為:
Signature ok
subject=/C=CN/ST=BeiJing/L=BeiJing/O=MyCompany Corp./CN=www.mycompany.com/emailAddress=admin@mycompany.com
Getting CA Private Key
Enter pass phrase for root.key: ← 輸入上面創建的密碼
4.導出.p12文件 client.p12
#生成命令
openssl pkcs12 -export -in /tmp/ca/client.crt -inkey /tmp/ca/client.key -out /tmp/ca/client.p12 -name "client"
根據命令提示,輸入client.key密碼,創建p12密碼。
四、配置Tomcat
1.關閉tomcat
tomcat的bin目錄下執行
shutdown.sh
2.將證書放入web服務器
#將keystore文件(server.keystore) 放在web服務器上 cp /tmp/ca/server.keystore /你的tomcat根目錄/conf #將truststore文件(root.truststore) 放在web服務器上 cp /tmp/ca/root.truststore /你的tomcat根目錄/conf
3.修改server.xml配置文件
cd /你的tomcat根目錄/conf vi server.xml
找到下面被注釋的代碼,刪除注釋符並修改內容(vi命令操作)
<Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol" maxThreads="150" SSLEnabled="true" scheme="https" secure="true" keystoreFile="/你的路徑/apache-tomcat-8.5.42/conf/server.keystore" keystorePass="******" truststoreFile="/你的路徑/apache-tomcat-8.5.42/conf/root.truststore" truststorePass="******" clientAuth="false" sslEnabledProtocols="TLSv1.2" /> <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />
4.啟動tomcat
tomcat的bin目錄下執行
startup.sh
5.訪問https服務
測試訪問連接
https://localhost:8443/
https://192.168.1.1:8443/ 你的IP
