Spring Cloud 学习笔记 ——Spring Cloud Config 配置文件加解密(非对称加密)

2022-09-08 11:09:24

13.2.3 非对称加密

直接看步骤

  • 1.生成非对称加密密钥:
    在配置了 JDK 和 上节JCE 的情况下,运行如下命令:
C:\Users\Administrator>keytool -genkeypair -alias config-server -keyalg RSA -key
store F:\springcloudStudy\configRepository\config-server.keystore

在这里插入图片描述
密钥库口令我输入的是六个1:111111;然后其他直接回车,在最后一步填,结果如下:
在这里插入图片描述
打开文件:
在这里插入图片描述

  • 2.把生产的 config-server.keystore 放入项目的 resoures 目录下:
    在这里插入图片描述

  • 3.配置 bootstrap.properties 文件,注释原来的 encrypt.key=javaboy 的内容

# 密钥
#encrypt.key=javaboy

encrypt.key-store.location=config-server.keystore
encrypt.key-store.alias=config-server
encrypt.key-store.password=111111
encrypt.key-store.secret=111111
  • 4.访问 /encrypt/status 请求地址,查看运行状态
    在这里插入图片描述
  • 5.用 POSTMAN 生成非对称加密的密文,明文随便选的,是dev666,可以看到生成一大串密文
    在这里插入图片描述
  • 6.把生成的密文复制到 client1-dev.properties,替换 javaboy 的值,然后提交到 git 仓库;
    在这里插入图片描述
    在这里插入图片描述
  • 7.再次访问http://localhost:8081/client1/dev/master接口:
    在这里插入图片描述
    用 config-client 服务访问接口:
    在这里插入图片描述

发现已经都解密成dev666了,这就是非对称加密,它需要 JCE 不限制长度加密 jar 包。可以从 Config Server 实时从 Git 上获取最新的配置文件,不需要重启服务。

  • 8.另外,如果在 classpath 中找不 config-server.keystore 文件,需要在 pom.xml 配置如下内容,我的项目自动加载了,不知道咋回事,没有的话配置如下内容:
<build><resources><resource><directory>src/main/resources</directory><includes><include>**/*.properties</include><include>**/*.keystore</include></includes></resource><build>
  • 作者:厕所博士
  • 原文链接:https://blog.csdn.net/x123453316/article/details/109317581
    更新时间:2022-09-08 11:09:24