背景
线上项目规定不能在配置文件中出现帐号、密码的明文信息,所以必须加密。
引入 Nacos Config 配置后,Nacos Config 帐号密码、加密,服务注册发现也用相同的 nacos 帐号密码,那么如何解密才能保证 Nacos Config 服务能够正确启动呢?
本文记录这个问题的解决思路。
解决方案
首先,跟踪 Nacos Config 的启动流程,关键配置信息是 NacosConfigProperties 这个类控制的,所以需要让它能加载到解密的信息,才能保证配置管理服务启动成功。
其次,它在 spring.cloud.nacos.config 下面没有配置帐号密码时,会从环境变量中加载信息。
第三,服务注册发现配置属性 NacosDiscoveryProperties,spring.cloud.nacos.discovery 未配置帐号密码的情况下,也会从环境变量中获取 nacos 全局的帐号和密码。
第四个技术点,可以通过 environment.getPropertySources() 对象操作系统环境变量,并对环境变量进行修改。
所以解决步骤是:
- 在 bootstrap.yml 配置的
spring.cloud.nacos.config下面配置 Nacos 帐号密码,且加密。
typescript
nacos:
# nacos 服务器地址
server-addr: xxx:8848
# nacos 配置中心,Nacos 帐号、密码
config:
enabled: true
username: 对称加密结果
password: 对称加密结果
- 自定义一个
org.springframework.cloud.bootstrap.BootstrapConfiguration类,并在 spring.factories 中添加声明。
typescript
org.springframework.cloud.bootstrap.BootstrapConfiguration=\xxx.MyBootStrapConfiguration
MyBootStrapConfiguration的构造函数中,对加密的 nacos 帐号密码进行解密,并回写到四个环境变量中。
java
String username = environment.getProperty("spring.cloud.nacos.config.username");
String password = environment.getProperty("spring.cloud.nacos.config.password");
// TODO 解密
// 解密结果存储到新的配置集合
Map<String, Object> newConfigMap = new HashMap<>();
newConfigMap.put("spring.cloud.nacos.config.username", username);
newConfigMap.put("spring.cloud.nacos.config.password", password);
// 更新 nacos.username/password 用于服务发现配置
newConfigMap.put("spring.cloud.nacos.username", username);
newConfigMap.put("spring.cloud.nacos.password", password);
// 这里将加密配置加载到配置列表的第一个位置,优先级最高;类型为 MapPropertySource,name 名称随意。
MutablePropertySources propertySources = environment.getPropertySources();
propertySources.addFirst(new MapPropertySource("myConfig", newConfigMap));
注意事项:
@ConfigurationProperties("spring.cloud.nacos.discovery")NacosDiscoveryProperties这个配置类会监控配置变化,但是它的配置信息直接从 yml 中加载的,如果帐号密码直接配置后,就不会从 Environment 对象中加载了,所以不能配置nacos.discovery.username/password属性。BootstrapConfiguration级别的类,框架内置了NacosConfigBootstrapConfiguration,这个类引用了 nacos.config 配置对应的属性类NacosConfigProperties,所以自定义的启动类的优先级必须比内置的NacosConfigBootstrapConfiguration高,使用@Order属性设置最高优先级,在真正的 Nacos 服务类启动之前对帐号密码的环境变量进行偷梁换柱。
自定义的 Bootstrap 启动配置类,完整配置如下:
typescript
@Order(Ordered.HIGHEST_PRECEDENCE)
public class MyBootStrapConfiguration {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* 依赖系统配置环境变量,需要偷梁换柱,解密掉加密配置
*/
@Autowired
private ConfigurableEnvironment environment;
@PostConstruct
public void decryptNacosConfig() {
String username = environment.getProperty("spring.cloud.nacos.config.username");
String password = environment.getProperty("spring.cloud.nacos.config.password");
// TODO 解密
// 解密结果存储到新的配置集合
Map<String, Object> newConfigMap = new HashMap<>();
newConfigMap.put("spring.cloud.nacos.config.username", username);
newConfigMap.put("spring.cloud.nacos.config.password", password);
// 更新 nacos.username/password 用于服务发现配置
newConfigMap.put("spring.cloud.nacos.username", username);
newConfigMap.put("spring.cloud.nacos.password", password);
// 这里将加密配置加载到配置列表的第一个位置,优先级最高;类型为 MapPropertySource,name 名称随意。
MutablePropertySources propertySources = environment.getPropertySources();
propertySources.addFirst(new MapPropertySource("myConfig", newConfigMap));
}
}
延伸思考知识
typescript
public NacosConfigProperties nacosConfigProperties(ApplicationContext context) {
if (context.getParent() != null
&& BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
context.getParent(), NacosConfigProperties.class).length > 0) {
return BeanFactoryUtils.beanOfTypeIncludingAncestors(context.getParent(),
NacosConfigProperties.class);
}
return new NacosConfigProperties();
}
跟踪过程中发现 MutablePropertySources 有很多不同类型的属性资源,这些配置的区别是什么?

启示录
typescript
@AutoConfigureBefore(NacosConfigBootstrapConfiguration.class)
无效
这个对于需要注册 org.springframework.cloud.bootstrap.BootstrapConfiguration 这种类型的自动注入类来说,是无效的。
有效的方式是通过 @Order(Ordered.HIGHEST_PRECEDENCE) 提升它的优先级。