1.EncryptPropertyPlaceholderConfigurer
自定义PropertyPlaceholderConfigurer继承PropertyPlaceholderConfigurer
package com.ruoyi.web;
import com.ruoyi.common.utils.StringUtils;
import org.omg.CORBA.Environment;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.ConfigurableEnvironment;
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;
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 = "0a9d3b9549ad7e8248433a76475cc128"; // 密钥
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定义的datasource获取
// 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;
}
}
2.ResourcePlaceholderConfig
项目启动时加载application.yml
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
@Configuration
public class ResourcePlaceholderConfig {
@Bean
public PropertySourcesPlaceholderConfigurer propertyConfigurer() {
PropertySourcesPlaceholderConfigurer config = new EncryptPropertyPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("application-druid.yml"));
config.setProperties(yaml.getObject());
return config;
}
}
相关代码资料下载地址:https://download.csdn.net/download/DaLiShuiJiao/87542289