有奖捉虫:办公协同&微信生态&物联网文档专题 HOT
配置加密功能提供了对配置值加密的存储全套解决方案,通过增强源生 SDK 能力,同时兼容本地文件配置和分布式配置的配置值加密。

准备工作

1. 确保使用最新的 TSF SDK,参见 Spring Cloud 应用概述
2. 按照 配置管理 添加了代码注释。
3. 下载 SDK 加密工具
4. 准备需要加密的相关信息(此处为举例,用户使用时请调整)
密码明文(plaintext):TX_PwDemO_1hblsqT
密钥(encypt password):encyptPassword

SDK 加密工具

1. 找到加密工具包(spring-cloud-tsf-encrypt-1.1.1-RELEASE.jar)。
2. 执行以下命令对配置明文密码进行加密(需升级到 Java8 161或以上版本,或使用 补丁 解决问题):
D:\\repo\\com\\tencent\\tsf\\spring-cloud-tsf-encrypt\\1.1.1-RELEASE>java -jar spring-cloud-tsf-encrypt-1.1.1-RELEASE.jar encrypt TX_PwDemO_1hblsqT encryptPassword
输出结果:
[encrypt] result:
3M7wGw2XtFc5Y+rxOgNBLrm2spUtgodjIxa+7F3XcAo=
用例:
D:\\repo\\com\\tencent\\tsf\\spring-cloud-tsf-encrypt\\1.1.1-RELEASE>java -jar spring-cloud-tsf-encrypt-1.1.1-RELEASE.jar
At least 3 arguments required. Usage: [operation] [content] [password]
[operation]: Choose one from [encrypt | decrypt].
[content]: Plaintext when encrypt or ciphertext when decrypt.
[password]: Encrypt or decrypt password.
3. 执行以下命令对密文密码进行解密:
D:\\repo\\com\\tencent\\tsf\\spring-cloud-tsf-encrypt\\1.1.1-RELEASE>java -jar spring-cloud-tsf-encrypt-1.1.1-RELEASE.jar decrypt 3M7wGw2XtFc5Y+rxOgNBLrm2spUtgodjIxa+7F3XcAo= encryptPassword
输出结果:
[decrypt] result:
TX_PwDemO_1hblsqT
用例:
D:\\repo\\com\\tencent\\tsf\\spring-cloud-tsf-encrypt\\1.1.1-RELEASE>java -jar spring-cloud-tsf-encrypt-1.1.1-RELEASE.jar
At least 3 arguments required. Usage: [operation] [content] [password]
[operation]: Choose one from [encrypt | decrypt].
[content]: Plaintext when encrypt or ciphertext when decrypt.
[password]: Encrypt or decrypt password.

配置项填写方式

注意
本地配置和线上配置同时支持(需要符合 spring-config 源生规范)。
本地 YAML
配置在 application.yml或application-*.yml:
tsf:
inventory:
password:
encrypt1: ENC(3M7wGw2XtFc5Y+rxOgNBLrm2spUtgodjIxa+7F3XcAo=)
配置中心 YAML
配置在全局配置/应用配置,并发布:
tsf:
inventory:
password:
encrypt2: ENC(3M7wGw2XtFc5Y+rxOgNBLrm2spUtgodjIxa+7F3XcAo=)
本地 Properties
配置在 application.properties或application-*.properties:
tsf.inventory.password.encrypt3=ENC(3M7wGw2XtFc5Y+rxOgNBLrm2spUtgodjIxa+7F3XcAo=)

业务应用使用

环境变量(推荐)
在系统环境变量中配置密钥(password):此时密钥泄露的风险最小。
tsf_config_encrypt_password=encryptPassword
JVM 参数(不推荐)
也可以在JVM参数中配置密钥(password):
-Dtsf_config_encrypt_password=encryptPassword
启动参数(不推荐)
也可以在应用启动参数中配置密钥(password):
--tsf_config_encrypt_password=encryptPassword
Java 测试代码
Java代 码按照常规配置使用。
配置类:
@ConfigurationProperties("tsf.inventory.password")
@Component
@RefreshScope
public class PasswordConfiguration {

private String encrypt1;
private String encrypt2;
private String encrypt3;

@Value("${tsf.inventory.password.encrypt1}")
private String encrypt4;
@Value("${tsf.inventory.password.encrypt2}")
private String encrypt5;
@Value("${tsf.inventory.password.encrypt3}")
private String encrypt6;

// getters and setters
}
测试类:

@RestController
public class TestController {

@Autowired
private PasswordConfiguration pwConfig;
/**
* 显示明文密码
*
* @return 明文密码
*/
@RequestMapping("/inventory/password")
public String showPassword() {
String content = "TX_PwDemO_1hblsqT";
StringBuffer sb = new StringBuffer("Test Config Encrypt/Decrypt:\\n");
// 内存读取
sb.append(String.format("[%s]\\t内存读取*.yml文件配置: %s\\n",
content.equals(
SpringCloudTsfApplication.ctx.getEnvironment().getProperty("tsf.inventory.password.encrypt1")),
SpringCloudTsfApplication.ctx.getEnvironment().getProperty("tsf.inventory.password.encrypt1")));
sb.append(String.format("[%s]\\t内存读取consul配置: %s\\n",
content.equals(
SpringCloudTsfApplication.ctx.getEnvironment().getProperty("tsf.inventory.password.encrypt2")),
SpringCloudTsfApplication.ctx.getEnvironment().getProperty("tsf.inventory.password.encrypt2")));
sb.append(String.format("[%s]\\t内存读取*.properties文件配置: %s\\n",
content.equals(
SpringCloudTsfApplication.ctx.getEnvironment().getProperty("tsf.inventory.password.encrypt3")),
SpringCloudTsfApplication.ctx.getEnvironment().getProperty("tsf.inventory.password.encrypt3")));
// Bean读取
sb.append(String.format("[%s]\\tBean读取*.yml文件配置: %s\\n", content.equals(pwConfig.getEncrypt1()),
pwConfig.getEncrypt1()));
sb.append(String.format("[%s]\\tBean读取consul配置: %s\\n", content.equals(pwConfig.getEncrypt2()),
pwConfig.getEncrypt2()));
sb.append(String.format("[%s]\\tBean读取*.properties文件配置: %s\\n", content.equals(pwConfig.getEncrypt3()),
pwConfig.getEncrypt3()));
// @Value读取
sb.append(String.format("[%s]\\t@Value读取*.yml文件配置: %s\\n", content.equals(pwConfig.getEncrypt4()),
pwConfig.getEncrypt4()));
sb.append(String.format("[%s]\\t@Value读取consul配置: %s\\n", content.equals(pwConfig.getEncrypt5()),
pwConfig.getEncrypt5()));
sb.append(String.format("[%s]\\t@Value读取*.properties文件配置: %s\\n", content.equals(pwConfig.getEncrypt5()),
pwConfig.getEncrypt5()));
return sb.toString();
}
}
输出结果如下:
Test Config Encrypt/Decrypt:
[true] 内存读取*.yml文件配置: TX_PwDemO_1hblsqT
[true] 内存读取consul配置: TX_PwDemO_1hblsqT
[true] 内存读取*.properties文件配置: TX_PwDemO_1hblsqT
[true] Bean读取*.yml文件配置: TX_PwDemO_1hblsqT
[true] Bean读取consul配置: TX_PwDemO_1hblsqT
[true] Bean读取*.properties文件配置: TX_PwDemO_1hblsqT
[true] @Value读取*.yml文件配置: TX_PwDemO_1hblsqT
[true] @Value读取consul配置: TX_PwDemO_1hblsqT
[true] @Value读取*.properties文件配置: TX_PwDemO_1hblsqT**