欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring-boot https證書(shū)雙向認(rèn)證配置的實(shí)現(xiàn)

 更新時(shí)間:2025年08月22日 10:17:53   作者:楊小熊的筆記  
本文詳細(xì)介紹SpringBoot項(xiàng)目中配置自簽名CA證書(shū)、簽發(fā)服務(wù)端和客戶(hù)端證書(shū),生成PKCS12格式的證書(shū),及設(shè)置SSL/TLS配置,實(shí)現(xiàn)雙向認(rèn)證,具有一定的參考價(jià)值,感興趣的可以了解一下

本文主要介紹在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ā)客戶(hù)端證書(shū)

生成客戶(hù)端證書(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)證客戶(hù)端證書(shū)
openssl verify -verbose -CAfile rootca.crt client.crt

4. 生成PKCS12服務(wù)端證書(shū)

通過(guò) openssl 命令生成服務(wù)端用的證書(shū) server.p12 和客戶(hù)端用到的證書(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客戶(hù)端證書(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)依賴(lài)和配置:

pom.xml

        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk18on</artifactId>
            <version>1.78.1</version>
        </dependency>

啟動(dòng)類(lèi):

    static {
        Security.addProvider(new BouncyCastleProvider());
    }

6. 驗(yàn)證請(qǐng)求

使用curl命令和使用postman均可以驗(yàn)證,需要配置客戶(hù)端證書(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ū)。

源代碼地址github

源代碼地址gitee

到此這篇關(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)文章

最新評(píng)論