spring-boot https證書(shū)雙向認(rèn)證配置的實(shí)現(xiàn)
本文主要介紹在spring-boot工程中配置https證書(shū)雙向認(rèn)證。包含生成自簽名證書(shū)命令,配置yml等。
注意:
- 該文章使用自簽名證書(shū),僅作為開(kāi)發(fā)驗(yàn)證使用,實(shí)際現(xiàn)網(wǎng)場(chǎng)景請(qǐng)從CA機(jī)構(gòu)申請(qǐng)證書(shū)。
- 文章中的命令均在linux環(huán)境下執(zhí)行。
- openssl版本為 OpenSSL 1.1.1k FIPS 25 Mar 2021。
- keytool對(duì)應(yīng)jre版本為 1.8.0_401。
1. 創(chuàng)建CA證書(shū)
創(chuàng)建CA證書(shū)。相關(guān)文件:
- rootca.key CA證書(shū)私鑰。
- rootca.crt CA證書(shū)。
- rootca.p12 PKCS12格式的信任證書(shū)庫(kù)。
- truststore.jks JKS格式的信任證書(shū)庫(kù)。
# 1. 創(chuàng)建CA私鑰 RootCaKey@2024 生成 rootca.key openssl genpkey -aes256 -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out rootca.key -pass pass:'RootCaKey@2024' # 2. 創(chuàng)建CA證書(shū)請(qǐng)求 生成 rootca.crt openssl req -x509 -days 3650 -sha256 -key rootca.key -passin pass:'RootCaKey@2024' -out rootca.crt -subj "/C=CN/CN=demorootca.demo.com" ## 生成并導(dǎo)入信任證書(shū)庫(kù) PKCS12 生成 rootca.p12 TrustStore@2024 keytool -import -noprompt -trustcacerts -alias rootca -file rootca.crt -keystore rootca.p12 -storetype PKCS12 -storepass 'TrustStore@2024' ### 查看 keytool -list -v -keystore rootca.p12 -storetype PKCS12 -storepass 'TrustStore@2024' ## 生成并導(dǎo)入信任證書(shū)庫(kù) JKS 生成 truststore.jks keytool -import -noprompt -trustcacerts -alias rootca -file rootca.crt -keystore truststore.jks -storetype JKS -storepass 'TrustStore@2024' ### 查看 keytool -list -v -keystore truststore.jks -storetype JKS -storepass 'TrustStore@2024'
2. 簽發(fā)服務(wù)端證書(shū)
生成服務(wù)端證書(shū) server.crt 并且使用ca證書(shū)簽發(fā)??梢允褂妹铗?yàn)證。
openssl genpkey -aes256 -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out server.key -pass pass:'ServerKey@2024' openssl req -new -key server.key -passin pass:'ServerKey@2024' -out server.csr -subj "/C=CN/CN=server.demo.com" openssl x509 -req -in server.csr -CA rootca.crt -CAkey rootca.key -passin pass:'RootCaKey@2024' -CAcreateserial -out server.crt -days 3650 -sha256 ## 使用CA證書(shū)驗(yàn)證服務(wù)端證書(shū) openssl verify -verbose -CAfile rootca.crt server.crt
3. 簽發(fā)客戶端證書(shū)
生成客戶端證書(shū) client.crt 并且使用ca證書(shū)簽發(fā)??梢允褂妹铗?yàn)證。
openssl genpkey -aes256 -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out client.key -pass pass:'ClientKey@2024' openssl req -new -key client.key -passin pass:'ClientKey@2024' -out client.csr -subj "/C=CN/CN=client.demo.com" openssl x509 -req -in client.csr -CA rootca.crt -CAkey rootca.key -passin pass:'RootCaKey@2024' -CAcreateserial -out client.crt -days 3650 -sha256 ## 使用CA證書(shū)驗(yàn)證客戶端證書(shū) openssl verify -verbose -CAfile rootca.crt client.crt
4. 生成PKCS12服務(wù)端證書(shū)
通過(guò) openssl 命令生成服務(wù)端用的證書(shū) server.p12 和客戶端用到的證書(shū) client.p12??梢允褂?keytool 命令查看。
即
- server.key+server.crt+rootca.crt -> server.p12
- client.key+client.crt+rootca.crt -> client.p12
openssl pkcs12 -export -inkey server.key -passin pass:'ServerKey@2024' -in server.crt -chain -CAfile rootca.crt -out server.p12 -password pass:'ServerKeyStore@2024' ## 查看 server.p12證書(shū) keytool -list -v -keystore server.p12 -storepass 'ServerKeyStore@2024' # 生成PKCS12客戶端證書(shū) openssl pkcs12 -export -inkey client.key -passin pass:'ClientKey@2024' -in client.crt -chain -CAfile rootca.crt -out client.p12 -password pass:'ClientKeyStore@2024' ## 查看 client.p12 證書(shū) keytool -list -v -keystore client.p12 -storepass 'ClientKeyStore@2024'
5. 配置spring-boot工程
將前邊生成的證書(shū) server.p12,truststore.jks,rootca.p12文件,拷貝到 src/main/resources/cert/ 目錄下。并修改 application.yml,內(nèi)容如下:
使用rootca.p12
server:
ssl:
enabled: true
key-store: classpath:cert/server.p12
key-store-password: 'ServerKeyStore@2024'
key-store-type: PKCS12
key-store-provider: BC
enabled-protocols: TLSv1.2,TLSv1.3
trust-store: classpath:cert/rootca.p12
trust-store-password: 'TrustStore@2024'
trust-store-type: PKCS12
trust-store-provider: BC
client-auth: need
servlet:
context-path: /SpringBoot2Demo
port: 8888
logging:
level:
root: info
使用 truststore.jks
server:
ssl:
enabled: true
key-store: classpath:cert/server.p12
key-store-password: 'ServerKeyStore@2024'
key-store-type: PKCS12
key-store-provider: BC
enabled-protocols: TLSv1.2,TLSv1.3
trust-store: classpath:cert/truststore.jks
trust-store-password: 'TrustStore@2024'
trust-store-type: JKS
trust-store-provider: SUN
client-auth: need
servlet:
context-path: /SpringBoot2Demo
port: 8888
logging:
level:
root: info
由于要使用到 BouncyCastleProvider,需要添加相關(guān)依賴和配置:
pom.xml
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk18on</artifactId>
<version>1.78.1</version>
</dependency>
啟動(dòng)類:
static {
Security.addProvider(new BouncyCastleProvider());
}
6. 驗(yàn)證請(qǐng)求
使用curl命令和使用postman均可以驗(yàn)證,需要配置客戶端證書(shū)。
# 使用命令驗(yàn)證 curl -k --cert-type P12 --cert ./client.p12:'ClientKeyStore@2024' --location --request GET 'https://localhost:8888/SpringBoot2Demo/demo/current'
使用postman的時(shí)候,需要將 Settings->General->REQUEST->SSL certificate verification 開(kāi)關(guān)關(guān)掉,即不校驗(yàn)SSL服務(wù)端證書(shū)。
到此這篇關(guān)于spring-boot https證書(shū)雙向認(rèn)證配置的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)spring-boot https雙向認(rèn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)過(guò)濾器攔截器的耗時(shí)對(duì)比
這篇文章主要為大家詳細(xì)介紹了SpringBoot實(shí)現(xiàn)過(guò)濾器攔截器的輸出接口耗時(shí)對(duì)比,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-06-06
Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決
這篇文章主要介紹了Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
SpringCloud Nacos作為配置中心超詳細(xì)講解
這篇文章主要介紹了Springcloud中的Nacos作為配置中心,本文以用戶微服務(wù)為例,進(jìn)行統(tǒng)一的配置,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
Mybatis中mapper.xml實(shí)現(xiàn)熱加載介紹
大家好,本篇文章主要講的是Mybatis中mapper.xml實(shí)現(xiàn)熱加載介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
java實(shí)現(xiàn)計(jì)算地理坐標(biāo)之間的距離
java實(shí)現(xiàn)計(jì)算地理坐標(biāo)之間的距離,主要是通過(guò)計(jì)算兩經(jīng)緯度點(diǎn)之間的距離來(lái)實(shí)現(xiàn),有需要的小伙伴參考下吧2015-03-03

