配置SpringBoot實(shí)現(xiàn)TLS雙向認(rèn)證

單向認(rèn)證

我們?cè)谕ǔTL問(wèn)一個(gè)網(wǎng)站,例如https://www.baidu.com,這是一個(gè)單向的TLS認(rèn)證,具體的過(guò)程為:服務(wù)器發(fā)送證書(shū)給客戶端,客戶端校驗(yàn)證書(shū)。驗(yàn)證證書(shū)有效之后,客戶端和服務(wù)器協(xié)商出一個(gè)對(duì)稱(chēng)加密密鑰由服務(wù)端的私鑰加密,客戶端收到之后再用公鑰解密這個(gè)對(duì)稱(chēng)密鑰,然后就開(kāi)始了傳輸層加密之旅。這種時(shí)候,服務(wù)端并不校驗(yàn)客戶端的合法性,來(lái)者不拒,絕大部分的網(wǎng)站都是這種類(lèi)型。

例如查看百度:

[root@iZbp1g905y8l5pclnbxvfxZ ~]# curl https://www.baidu.com -v
* About to connect() to www.baidu.com port 443 (#0)
*   Trying 180.101.49.11...
* Connected to www.baidu.com (180.101.49.11) port 443 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*       subject: CN=baidu.com,O="Beijing Baidu Netcom Science Technology Co., Ltd",OU=service operation department,L=beijing,ST=beijing,C=CN
*       start date: May 09 01:22:02 2019 GMT
*       expire date: Jun 25 05:31:02 2020 GMT
*       common name: baidu.com
*       issuer: CN=GlobalSign Organization Validation CA - SHA256 - G2,O=GlobalSign nv-sa,C=BE
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2443
< Content-Type: text/html
< Date: Mon, 19 Aug 2019 05:56:19 GMT
< Etag: "588603eb-98b"
< Last-Modified: Mon, 23 Jan 2017 13:23:55 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新聞</a> <a href=https://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地圖</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>視頻</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>貼吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登錄</a> </noscript> <script>document.write('<a + encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登錄</a>');
                </script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多產(chǎn)品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>關(guān)于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必讀</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意見(jiàn)反饋</a>&nbsp;京ICP證030173號(hào)&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
* Connection #0 to host www.baidu.com left intact

雙向認(rèn)證

有時(shí)候我們?cè)谝恍┌踩砸筝^高的場(chǎng)景下,服務(wù)器也需要來(lái)校驗(yàn)客戶端的合法性。在客戶端驗(yàn)證了服務(wù)器證書(shū)的合法性之后,客戶端需要帶上自己的證書(shū),服務(wù)器收到證書(shū)之后,比對(duì)服務(wù)器在信任鏈中是否信任了客戶端的證書(shū),如果信任,則服務(wù)端校驗(yàn)客戶端合法。如果證書(shū)不在服務(wù)端的受信列表上,則拒絕服務(wù)。這樣子其實(shí)就是建立了一條雙向認(rèn)證的TLS傳輸通道。

配置SpringBoot的SSL

在SpringBoot中很容易就能做到雙向認(rèn)證的配置,具體如下(我們使用內(nèi)嵌tomcat):

server:
  port: 8443
  ssl:
    key-store: server.jks
    key-store-password: password
    key-store-type: PKCS12
    trust-store: server_trust.jks
    trust-store-type: JKS
    trust-store-password: password
 #需要認(rèn)證客戶端證書(shū)
    client-auth: need

關(guān)鍵就是兩組keyStore的生成,雙向認(rèn)證的情況下,首先服務(wù)器需要生成一對(duì)公私鑰,并請(qǐng)求CA簽發(fā)證書(shū)。證書(shū)通常配置在servlet容器,或者配置在前端的負(fù)載均衡服務(wù)器中。證書(shū)鏈(自己生成的根證書(shū)及自己簽發(fā)的中間證書(shū))或者CA根證書(shū)(操作系統(tǒng)中自帶的信任根證書(shū))需要轉(zhuǎn)換成JKS,并且配置在服務(wù)器的trustStore配置中,也可以同樣配置在負(fù)載均衡上。

另一對(duì)為客戶端生成的的keyStore(Java客戶端)或者包含公鑰私鑰的密鑰交換格式(p12)。第二對(duì)的公私鑰通常包含了對(duì)客戶端的一些信息定義。然后將公鑰發(fā)給CA,請(qǐng)CA簽發(fā)一張證書(shū)。這個(gè)CA可以是自簽名的,也可以是第三方的證書(shū)機(jī)構(gòu)。然后在請(qǐng)求之后帶上發(fā)給服務(wù)端

生成keystore的命令參考如下

keytool -importkeystore -srckeystore keystore.p12 -srcstoretype PKCS12 -deststoretype PKCS12 -destkeystore keystore.jks

測(cè)試證書(shū)配置是否正確

OpenSSL提供了一個(gè)命令來(lái)驗(yàn)證證書(shū)的配置是否正確,具體如下:

openssl s_client -connect YOURHOST:443 -CAfile ca.pem -servername YOURHOST -key key.pem -cert cert.pem

其中-CAfile可選,在服務(wù)端配置的證書(shū)為自簽名證書(shū)的情況下,需要帶上這個(gè)自簽名的ca證書(shū)鏈。

項(xiàng)目遇到的坑總結(jié)

因?yàn)橹皩?duì)trustStore的理解不夠深刻,因此,在項(xiàng)目中配置server.ssl.trust-store時(shí)候,直接將PKCS12密鑰交換文件轉(zhuǎn)換過(guò)來(lái)的JKS設(shè)置成為trustStore。這里其實(shí)是有問(wèn)題的,trustStore是服務(wù)器的信任密鑰存儲(chǔ)庫(kù),存CA的證書(shū)(操作系統(tǒng)管理的所有受信任的根證書(shū)),有一部分人存的是客戶端證書(shū)集合(比如我們內(nèi)部自己的自簽名證書(shū),必須手動(dòng)設(shè)置為信任)不算特別規(guī)范,但是trustStore里是絕對(duì)不能有私鑰信息的。否則在加載trustStore的時(shí)候會(huì)報(bào)類(lèi)似錯(cuò)誤(spring-boot-2.1.0+內(nèi)嵌tomcat):

Caused by: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
    at java.security.cert.PKIXParameters.setTrustAnchors(PKIXParameters.java:200) ~[na:1.8.0_181]
    at java.security.cert.PKIXParameters.<init>(PKIXParameters.java:157) ~[na:1.8.0_181]
    at java.security.cert.PKIXBuilderParameters.<init>(PKIXBuilderParameters.java:130) ~[na:1.8.0_181]
    at org.apache.tomcat.util.net.jsse.JSSEUtil.getParameters(JSSEUtil.java:390) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
    at org.apache.tomcat.util.net.jsse.JSSEUtil.getTrustManagers(JSSEUtil.java:314) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
    at org.apache.tomcat.util.net.AbstractJsseEndpoint.createSSLContext(AbstractJsseEndpoint.java:112) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
    ... 24 common frames omitted

一開(kāi)始我也一頭霧水,網(wǎng)上找了很多方法,壓根就不管用。后來(lái)還是決定跟到代碼中看原因,發(fā)現(xiàn)了這么一個(gè)判斷:

image.png

因此我有理由相信,這個(gè)trustStore的內(nèi)容一定出問(wèn)題了。所以,我嘗試只用證書(shū)鏈來(lái)生成trustStore:
導(dǎo)入我們的證書(shū)鏈(從根證書(shū)到應(yīng)用證書(shū))

keytool -import -alias ourtrust -file our_trust_certificates_chain.pem -keystore our_trust.jks

然后,執(zhí)行我們的測(cè)試方法:

void sslCall() throws Exception {

    char[] password = "password".toCharArray();
    // 開(kāi)發(fā)環(huán)境中,不一定會(huì)有域名,因此可能會(huì)造成證書(shū)域名和真實(shí)服務(wù)器IP無(wú)法匹配而校驗(yàn)失敗。
    //因此在開(kāi)發(fā)環(huán)境中,客戶端需要加上這么一段配置用來(lái)跳過(guò)服務(wù)端證書(shū)校驗(yàn)
    TrustStrategy acceptingTrustStrategy = (X509Certificate[] x509Certificates, String s) -> true;
    SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom()
            // 配置信任鏈
            .loadTrustMaterial(null, acceptingTrustStrategy)
            .loadKeyMaterial(keyStore("classpath:client_keystore.jks", password), password)
            .build();
    SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
    CloseableHttpClient httpClient = HttpClients.custom()
            .setSSLSocketFactory(csf)
            .build();
    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setHttpClient(httpClient);

    RestTemplate restTemplate = new RestTemplate(requestFactory);
    ResponseEntity<String> response = restTemplate.exchange("https://localhost:7099/api/login", HttpMethod.GET, null, String.class);
    System.out.println(response);
}

這樣就能正確通過(guò)雙向認(rèn)證了。

測(cè)試CURL命令

curl -k --cert cert.pem --key key.pem https://mine.host.com/api/aloha -v
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容