参考工程地址:https://gitee.com/proLeo/ray,配置如下
自定义PropertySourcesPlaceholderConfigurer 解密,其中使用了自定义的私钥名称enc.key,在需要加密的yml配置项前加入自定义的前缀enc:,表示该项需要解密,类中的AESUtil可根据需要自己定义 package com.mrray.ray.common.plugin; import com.mrray.ray.common.AESUtil; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertiesPropertySource; import java.io.IOException; import java.util.Map; import java.util.Properties; /** * 配置解密,只能自定义一个PropertySourcesPlaceholderConfigurer,否则会报异常 * * @author lyc **/ public class EncryptPropertyPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer implements InitializingBean { /** * 需要解密的配置项前缀 */ private static final String PREFIX_ENC = "enc:"; private Environment environment; @Override public void setEnvironment(Environment environment) { this.environment = environment; } @Override protected Properties mergeProperties() throws IOException { Properties mergedProperties = new Properties(); for (Properties localProp : localProperties) { mergedProperties.putAll(localProp); } for (Map.Entry entry : mergedProperties.entrySet()) { if (entry.getValue().toString().startsWith(PREFIX_ENC)) { String key = System.getProperty("enc.key"); String value = entry.getValue().toString().replace(PREFIX_ENC, StringUtils.EMPTY); mergedProperties.setProperty(entry.getKey().toString(), AESUtil.decode(value, key)); } } //针对sharding-jdbc datasource自定义解密的特殊处理 //因为sharding-jdbc的datasource注入是从environment中获取propertySource, //不能直接通过PropertySourcesPlaceholderConfigurer定义的resource获取 MutablePropertySources sources = ((ConfigurableEnvironment) environment).getPropertySources(); sources.addFirst(new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergedProperties)); return mergedProperties; } @Override public void afterPropertiesSet() throws Exception { localOverride = true; } } sharding: jdbc: dataSource: names: master0,master1,master0-slave1,master0-slave2,master1-slave1 master0: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://192.168.125.161:36005/iflow?autoReconnect=true&useUnicode=true&createDatabaseIfNotExist=true&characterEncoding=utf8&useSSL=false&serverTimezone=CTT username: root #使用了自定义的AES对称加密,启动时需要添加启动参数-Denc.key=${自定义的key},用ray-common中的AESUtil加密 #如果不需要去除enc:前缀即可 password: enc:jEhi6SfY38B7rIB9wrFZ1w== driver-class-name: com.mysql.cj.jdbc.Driver 接管配置文件解析 @Configuration public class ResourcePlaceholderConfig { @Bean public PropertySourcesPlaceholderConfigurer propertyConfigurer() { PropertySourcesPlaceholderConfigurer config = new EncryptPropertyPlaceholderConfigurer(); YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean(); yaml.setResources(new ClassPathResource("application-dev.yml")); config.setProperties(yaml.getObject()); return config; } }