微服务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

猜你喜欢

转载自www.cnblogs.com/linjiqin/p/10338588.html