随笔 - 1357  文章 - 0  评论 - 1104  阅读 - 1941万

微服务SpringCloud—Config Server对称加密

配置内容的加解密
在Git仓库中明文存储配置属性的。很多场景下,对于某些敏感的配置内容(例如数据库账号、密码等),应当加密存储。

Config对称加解密
1、安装JCE
默认情况下我们的JRE自带了JCE,但是默认是一个有限长度的版本,需要到oracle官网下载一个不限长度的JCE。

JCE下载地址
https://www.oracle.com/technetwork/java/javase/downloads/jce-all-download-5170447.html

下载JCE并解压(eg:jce_policy-8.zip),按照其中的README.txt的说明安装。

JCE的安装非常简单,其实就是将JDK/jre/lib/security目录中的两个jar文件(local_policy.jar、US_export_policy.jar)替换为压缩包中的jar文件。

2、在config server服务的bootstrap.yml文件中配置对称密匙

#博客:https://blog.csdn.net/u014296316/article/details/80881974
#http://localhost:6063/encrypt/status  验证加密解密功能是否正常
#http://localhost:6063/encrypt 只允许post请求
#http://localhost:6063/decrypt 只允许post请求
encrypt:
  key: Lynch

 

3、访问 http://localhost:6063/encrypt/status 验证加密解密功能是否正常

4、访问/encrypt和/decrypt进行加密解密
http://localhost:6063/encrypt 只允许post请求

http://localhost:6063/decrypt 只允许post请求

5、配置文件中使用{cipher}开头标识加密数据

6、在Config Client服务获取加密数据

复制代码
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 这边的@RefreshScope注解不能少,否则即使调用/refresh,配置也不会刷新
 */
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${env}")
    private String env;
    
    @Value("${password}")
    private String password;
    
    @Value("${username}")
    private String username;

    @GetMapping("/config/profile")
    public String hello() {
        return this.env+","+this.password+","+this.username;
    }
}
复制代码

 

 

http://localhost:6062/config/profile

posted on   Ruthless  阅读(2325)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
历史上的今天:
2012-01-30 二、oracle sql*plus常用命令
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示