1背景
在spring開發(fā)過程中,使用到配置文件中通常保護一些敏感信息橱夭,比如數(shù)據庫密碼,redis登陸密碼等.這些敏感信息直接明文保存在配置文件中是不太安全的.因此我們希望在配置文件中把敏感信息加密阱高,在運行時動態(tài)解密.
2實現(xiàn)思路
在具體實現(xiàn)之前,我們得有一個問題的解決思路.思考一下昆禽,我們通過@Value
(@ConfigurationProperties
也一樣)給一個bean注入屬性值,這個過程怎么實現(xiàn)的?在容器初始化過程先加載配置文件把這些值是保存在PropertySources
中蝇庭,然后容器實例化這個bean后醉鳖,從PropertySources
查找@Value
名稱對應的值,并把這個值賦給這個屬性.因此哮内,我們希望在容器加載完配置文件后盗棵,bean注入屬性之前,把加密的信息解密掉北发,這樣就可以達到動態(tài)解密的目的.
3具體實現(xiàn)
在實現(xiàn)思路里頭纹因,講到 容器加載完配置文件后,bean注入屬性之前琳拨,把加密的信息解密掉. 這就需要去擴展容器的實現(xiàn).在Spring中提供了BeanFactoryPostProcessor
.這個接口的語義與BeanPostProcessor
相似瞭恰,BeanFactoryPostProcessor
定義在ApplicationContext
會被自動執(zhí)行,以便運用于修改定義在容器中的元數(shù)據配置.解密的BeanFactoryPostProcessor
實現(xiàn)如下:
package com.simos.decrypt;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
import java.util.HashMap;
import java.util.Map;
/**
* Created by l2h on 18-5-8.
* Desc:BeanFactoryPostProcessor完成配置文件中屬性解密
* @author l2h
*/
public class SimosBeanFactoryPostProcessor implements BeanFactoryPostProcessor,BeanFactoryAware,Ordered {
/**
* 應用環(huán)境狱庇,用于獲取配置屬性
*/
private ConfigurableEnvironment environment;
private BeanFactory beanFactory;
/**
* 完成解密的接口
*/
private EncryptStringValueResolver valueResolver;
public SimosBeanFactoryPostProcessor(ConfigurableEnvironment environment ){
this.environment = environment;
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactoryToProcess) throws BeansException {
/**
* 獲取所有配置屬性
*/
valueResolver = (EncryptStringValueResolver)beanFactory.getBean("encryptStringValueResolver");
MutablePropertySources propertyValues = environment.getPropertySources();
decryptProperty(propertyValues,valueResolver);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
@Override
public int getOrder() {
return LOWEST_PRECEDENCE;
}
/**
* 解密方法.遍歷每個PropertySources惊畏,遍歷PropertySources的每個鍵值如果包含加密信息就解密,最后替換.
* @param propertyValues
* @param valueResolver
*/
private void decryptProperty(MutablePropertySources propertyValues,EncryptStringValueResolver valueResolver) {
propertyValues.forEach(pv->{
PropertySource propertySource = propertyValues.get(pv.getName());
if (propertySource instanceof MapPropertySource){
//保存處理過后的值
Map<String,Object> convertPropertySource = new HashMap<>();
//重新定義一個PropertySource密任,覆蓋原來的
MapPropertySource newMapPropertySource = new MapPropertySource(pv.getName(),convertPropertySource);
String[] propertyNames = ((MapPropertySource) propertySource).getPropertyNames();
for (int i=0,length = propertyNames.length;i<length;i++){
Object value = propertySource.getProperty(propertyNames[i]);
if (value instanceof String){
convertPropertySource.put(propertyNames[i], valueResolver.resolveEncryptStringValue((String) value));
}
else {
convertPropertySource.put(propertyNames[i],value);
}
}
//處理過后值替換原來的PropertySource
propertyValues.replace(pv.getName(),newMapPropertySource);
}
});
}
}
主要是通過ConfigurableEnvironment獲取所有的配置屬性陕截,SimosStringValueResolver包含具體解密實現(xiàn).
其他業(yè)務代碼實現(xiàn),不詳細講解,代碼均有說明.github傳送門:樣例源碼
小結
這個功能主要是使用BeanFactoryPostProcessor
接口批什,在Spring IoC容器初始化的過程添加一下自定義邏輯.