1. 程式人生 > 其它 >解決自簽名證書在Chrome上的“不是私密連線問題”

解決自簽名證書在Chrome上的“不是私密連線問題”

技術標籤:前端開發https

一、生成證書

1、建立根證書

新建MyCompanyCA.cnf檔案並輸入以下內容:

[ req ]
distinguished_name  = req_distinguished_name
x509_extensions     = root_ca

[ req_distinguished_name ]

# 以下內容可隨意填寫
countryName             = CN (2 letter code)
countryName_min         = 2
countryName_max         = 2
stateOrProvinceName     = ZheJiang
localityName            = HangZhou
0.organizationName      = Mycompany
organizationalUnitName  = technology 
commonName              = develop 
commonName_max          = 64
emailAddress            = 
[email protected]
emailAddress_max = 64 [ root_ca ] basicConstraints = critical, CA:true

新建MyCompanyLocalhost.ext檔案並輸入以下內容:

subjectAltName = @alt_names
extendedKeyUsage = serverAuth

[alt_names]

# 域名,如有多個用DNS.2,DNS.3…來增加
DNS.1 = domain.com 
# IP地址
IP.1 = 127.0.0.1

說明:
MyCompanyCA.cnf檔案是為申請CA根證書的配置檔案;

MyCompanyLocalhost.ext是生成伺服器證書的擴充套件配置檔案;

2、生成證書

執行以下3條命令:

openssl req -x509 -newkey rsa:2048 -out MyCompanyCA.cer -outform PEM -keyout MyCompanyCA.pvk -days 10000 -verbose -config MyCompanyCA.cnf -nodes -sha256 -subj "/CN=MyCompany CA"
openssl req -newkey rsa:2048 -keyout MyCompanyLocalhost.pvk -out MyCompanyLocalhost.req -subj /CN=localhost -sha256 -nodes
openssl x509 -req -CA MyCompanyCA.cer -CAkey MyCompanyCA.pvk -in MyCompanyLocalhost.req -out MyCompanyLocalhost.cer -days 10000 -extfile MyCompanyLocalhost.ext -sha256 -set_serial 0x1111

執行完成後,會得到,其中MyCompanyCA.cer是用來安裝在瀏覽器、安卓和蘋果裝置上,MyCompanyLocalhost.cer和MyCompanyLocalhost.pvk是放在服務端的證書和key檔案,在Nginx中配置即可.

-rw-r--r--  1 hugh  staff   1.0K  3  5 15:20 MyCompanyCA.cer
-rw-r--r--  1 hugh  staff   592B  3  5 16:03 MyCompanyCA.cnf
-rw-r--r--  1 hugh  staff   1.6K  3  5 15:20 MyCompanyCA.pvk
-rw-r--r--  1 hugh  staff   1.0K  3  5 15:20 MyCompanyLocalhost.cer
-rw-r--r--  1 hugh  staff   116B  3  5 15:20 MyCompanyLocalhost.ext
-rw-r--r--  1 hugh  staff   1.6K  3  5 15:20 MyCompanyLocalhost.pvk
-rw-r--r--  1 hugh  staff   891B  3  5 15:20 MyCompanyLocalhost.req

二、在裝置上安裝證書

以mac為例,直接雙擊MyCompanyCA.cer檔案或者在鑰匙串中匯入證書,證書匯入後,預設狀態如下:

開啟“信任”並選擇“永遠相信”

開啟要訪問的地址已經變為綠色”安全”