不了解jasypt的先去了解下,基本用法就是在数据库密码等敏感信息处进行加密处理;
例如: spring.redis.password=ENC(px98dK+6YrPE8F3eJcbatRLRwwy8TlzP)
然后需要配置密钥,可以直接在配置文件里配置,代码里配置;当然最安全的是配置到环境变量或启动时添加到启动参数;
下面仿照它,实现最基本的功能:
/**
* @Auther: gaoyang
* @Description:
*/
@Configuration
public class AuthConfig implements EnvironmentAware, BeanFactoryPostProcessor {
@Override
public void setEnvironment(Environment environment) {
ConfigurableEnvironment env = (ConfigurableEnvironment) environment;
MutablePropertySources propertySources = env.getPropertySources();
propertySources.stream()
.filter(var0 -> {
return var0.getSource() instanceof LinkedHashMap;
})
.filter(var1 -> {
return ((LinkedHashMap) var1.getSource())
.entrySet().stream().anyMatch(var2 -> {
if (var2 instanceof Map.Entry) {
return ((Map.Entry) var2).getValue().toString().startsWith("menpad~")
&& ((Map.Entry) var2).getValue().toString().endsWith("~");
}
return false;
});
})
.map(var3 -> {
((LinkedHashMap) var3.getSource())
.entrySet().stream().forEach(f -> {
String value = ((Map.Entry) f).getValue().toString();
if (value.startsWith("menpad~") && value.endsWith("~")) {
String old = value.replaceAll("menpad~", "")
.replaceAll("~", "");
//对参数进行解密处理,并赋值回去
//........
((Map.Entry) f).setValue(old);
}
});
return var3;
}).forEach(var4 -> {
propertySources.replace(var4.getName(), var4);
});
}
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
}
}
然后配置你的参数为:
password=menpad~xxxxxxxxxxxx~