先說雙向認證吧,需要使用openssl產生相應的認證(根憑證、服務端認證、多套用戶端認證)。(建議在linux環境下進行操作),我是centos7。
部署模式大體如下:
客戶瀏覽器(或其他工具)《==ssl 雙向認證的安全通道==》Nginx代理服務<-- 內網非安全通道 -->web服務(tomcat 或其他)叢集
一、Nginx雙向認證
1.產生根秘鑰及認證
openssl genrsa -des3 -out ca.key 2048
openssl req -new -x509 -days 3650 -key ca.key -out ca.crt -subj "/C=CN/ST=HA/L=ZZ/O=topxx/OU=internet/CN=www.xx.top/emailAddress=xxxxxxx@qq.com"
2.產生服務端認證
openssl genrsa -des3 -out www.xxx.com.pem 1024
openssl rsa -in queue.qmx.top.pem -out www.xxx.com.key
openssl req -new -key www.xxx.com.pem -out www.xxx.com.csr -subj "/C=CN/ST=HA/L=ZZ/O=xxx/OU=internet/CN=www.xxx.com/emailAddress=xxxxxx@qq.com"
3.根憑證簽名服務端認證
openssl ca -policy policy_anything -days 1460 -cert ca.crt -keyfile ca.key -in www.xxx.com.csr -out www.xxx.com.crt
cat ca.crt >> www.xxx.com.crt
4.產生用戶端認證 (需要在客戶機器上安裝)
openssl genrsa -des3 -out client.pem 2048
openssl req -new -key client.pem -out client-req.csr -subj "/C=CN/ST=XZ/L=LS/O=gsj/OU=gss/CN=www.clientdomain.com/emailAddress=xxxx@126.com"
openssl ca -policy policy_anything -days 1460 -cert ca.crt -keyfile ca.key -in client-req.csr -out client.crt
openssl pkcs12 -export -clcerts -in client.crt -inkey client.pem -out client.p12
客戶機器上雙擊p12檔案,進行安裝。如需要其他格式檔案,使用openssl匯出。
5.Nginx安裝(支援ssl模組,已安裝過的,可以搜尋教程,單單更新安裝ssl模組)
cd /usr/local/src #進入使用者目錄
wget http://nginx.org/download/nginx-1.15.0.tar.gz #下載最新版本nginx
tar -zxvf nginx-1.15.0.tar.gz #解壓
cd nginx-1.15.0#進入目錄
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-http_realip_module --with-http_image_filter_module #檢測
說明--prefix 指定安裝目錄
make #編譯
make install #安裝
6.Nginx ssl雙向認證配置
server {
listen 443;
server_name test.com www.test.com;
root html;
index index.html index.htm;
ssl on; #開啟ssl
ssl_certificate /usr/ssl/light.cn.crt; #伺服器憑證位置
ssl_certificate_key /usr/ssl/light.cn.key; #伺服器私密金鑰
ssl_client_certificate /usr/ssl/ca.crt; #CA認證用於驗證用戶端認證的合法性
ssl_verify_client on; #開啟對用戶端的驗證
ssl_session_timeout 5m; #session有效期間,5分鐘
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'AES128+EECDH:AES128+EDH:!aNULL'; #密碼編譯演算法
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
二、Golang https client 自我簽署憑證使用
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
pool := x509.NewCertPool()
addTrust(pool, "./ca.crt") //添加信任的認證,最好是服務端對應的根憑證
cliCrt, err := tls.LoadX509KeyPair("./client.crt", "./client.key")
if err != nil {
fmt.Println("Loadx509keypair err:", err)
return
}
tr := &http.Transport{
TLSClientConfig: &tls.Config{
RootCAs: pool,
Certificates: []tls.Certificate{cliCrt},
//InsecureSkipVerify: true, //跳過驗證服務端認證
},
}
client := &http.Client{Transport: tr}
resp, err := client.Get("https://www.xxx.com/myaction.do") //此處必須使用網域名稱或者host內的別名
if err != nil {
fmt.Println("Get error:", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
func addTrust(pool *x509.CertPool, path string) {
aCrt, err := ioutil.ReadFile(path)
if err != nil {
fmt.Println("ReadFile err:", err)
return
}
pool.AppendCertsFromPEM(aCrt)
}