当前位置:   article > 正文

springboot整合https使用自签名证书实现浏览器和服务器之间的双向认证

springboot整合https使用自签名证书实现浏览器和服务器之间的双向认证

效果描述:本地环境  两台以上电脑  可以实现安装客户端证书的电脑可以访问springboot启动项目,没有安装客户端证书的电脑无法访问springboot启动项目

1.操作:需要安装openssl工具

工具包:Win64OpenSSL_Light-3_3_0.exe 或者Win64OpenSSL_Light-3_3_0.msi 

官网:[ Downloads ] - /source/index.html (openssl.org)

验证安装OK: windows +r 输入cmd 打开dos窗口

输入openssl -v

出现这个表示安装成功

2.在d盘 目录 输入cmd 打开dos窗口执行一下命令 生成根证书 服务端证书(根证书签发) 客户端证书(根证书签发) 信任库 ,并将根证书导入信息库 主题-subj可以修改成自己想要的

# 生成根证书
openssl genrsa -out ca.key 2048
openssl req -x509 -new -nodes -key ca.key -subj "/CN=localhost/OU=BESTI/O=DKY/L=SY/ST=LN/C=CN" -days 365 -out ca.crt

# 生成服务端证书
openssl genrsa -out server.key 2048
openssl req -new -key server.key -subj "/CN=localhost/OU=BESTI/O=DKY/L=SY/ST=LN/C=CN" -out server.csr
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365
openssl pkcs12 -export -in server.crt -inkey server.key -out server.p12 -name MyServer -password pass:123456

# 生成客户端证书
openssl genrsa -out client.key 2048
openssl req -new -key client.key -subj "/CN=localhost/OU=BESTI/O=DKY/L=SY/ST=LN/C=CN" -out client.csr
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 365

openssl pkcs12 -export -in client.crt -inkey client.key -out client.p12 -name "MyClient" -password pass:123456

# 生成信任库并导入根证书
keytool -import -alias root -file ca.crt -keystore truststore.jks -storepass 123456

3.springboot项目配置

server:
  port: 8081
  ssl:
    enabled: true
    key-store-password: 123456     #密钥存储密码
    key-store: classpath:server.p12
    key-alias: MyServer
    client-auth: need # 需要客户端证书  将服务器证书库当作信任库来使用
    trust-store: classpath:truststore.jks
    trust-store-password: 123456
    trust-store-type: jks

4.在浏览器安装client.p12 客户端证书

5.通过https协议访问项目,注意选择证书

就可以访问项目了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/489856
推荐阅读
相关标签
  

闽ICP备14008679号