背景
公司规范要求配置文件里不能出现明文的密码。最近项目引入了Nacos作为服务的配置中心,使用的是spring-cloud-starter-alibaba-nacos-config
这个包。
基本的bootstrap.yaml
配置如下:
spring:
cloud:
nacos:
config:
server-addr: <host>:<port>
prefix: application
group: shared
namespace: xxx
file-extension: yaml
username: user
password: plain_text_password
......
那么如何将spring.cloud.nacos.config.password
换为公司内部加密算法加密后的密码呢?
研究
打开spring-cloud-starter-alibaba-nacos-config
的jar包,我们可以在META-INF/spring.factories
这个文件中看到以下内容:
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration
......
Nacos在Spring Cloud的bootstrap阶段可以进行自动配置就是在这里指定的。接下来找到NacosConfigBootstrapConfiguration
这个类,发现它获取配置的地