如何就解决配置文件脱敏

背景:
目前项目中,密码用户名都采用明文保存,为了安全考虑需要进行脱敏。
通过在 BeanFactoryPostProcessor 中,对属性进行解密。
遇到的困难:
在 BeanFactoryPostProcessor 中需要用到其他bean
解决办法:
通过 beanFactory中获取bean

@Slf4j
public class EncryptBeanFactoryPostProcessor implements BeanFactoryPostProcessor, Ordered {

    private ConfigurableEnvironment env;

    public EncryptBeanFactoryPostProcessor(ConfigurableEnvironment env) {
        this.env = env;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        MutablePropertySources propertySources = env.getPropertySources();
        EncryptionUtils encryption = beanFactory.getBean(EncryptionUtils.class);
        Map<String, Object> map = new HashMap<>();
        for (PropertySource<?> propertySource : propertySources) {
            if (propertySource instanceof MapPropertySource) {
                MapPropertySource source = (MapPropertySource) propertySource;
                //遍历所有的配置信息
                for (String name : source.getPropertyNames()) {
                    Object value = source.getProperty(name);
                    if (value instanceof String) {
                        String str = (String) value;
                        //包含加密前缀,截取去掉统一的加密前缀后的信息
                        if (str.startsWith("mpw:")) {
                            String st = encryption.encrypt(str.substring(5));
                            map.put(name, st);
                            log.info("Encrypt key:{},value:{}", name, st);
                        } else {
                            log.info("Encrypt key:{},value:{}", name, value);
                        }
                    }

                }
            }

        }

        if (!map.isEmpty()) {
            propertySources.addFirst(new MapPropertySource("custom-encrypt", map));
        }
    }


    @Override
    public int getOrder() {
        return Ordered.LOWEST_PRECEDENCE;
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值