spring 容器修改配置文件里值。解密扩展类

spring 容器修改配置文件里值。解密扩展类

1.实现 BeanFactoryPostProcessor
@Configuration
public class ApplicationContextInitializer {

@Bean
public BeanFactoryPostProcessor BeanFactoryPostProcessor() {
    return new BeanFactoryPostProcessor() {
        @Override
        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            MutablePropertySources propertySources = beanFactory.getBean(ConfigurableEnvironment.class).getPropertySources();
            if (!propertySources.contains("PropertySource")) {
                PropertySource PropertySource = new PropertySource(propertySources);
                propertySources.addFirst(PropertySource);
            }
        }
    };
}

}

  1. 定义PropertySource 类

public class PropertySource extends PropertySource {

public static final String PUB_KEY = "MIKSECPUBLIC";
private static final String PREFIX = "enc(";
private static final String SUFFIX = ")";

private static final Logger LOGGER = LoggerFactory.getLogger(PropertySource.class);

private MutablePropertySources originalPropertySource;


public PropertySource(MutablePropertySources originalPropertySource) {
    super("PropertySource", originalPropertySource);
    this.originalPropertySource = originalPropertySource;
}

@Override
public String getName() {
    return super.getName();
}

@Override
public Object getSource() {
    return super.getSource();
}

@Override
public boolean containsProperty(String name) {
    for (PropertySource propertySource : originalPropertySource) {
        if (propertySource instanceof PropertySource) {
            continue;
        }
        if (propertySource.containsProperty(name)) {
            return true;
        }
    }
    return false;
}

@Override
public boolean equals(Object other) {
    return super.equals(other);
}

@Override
public int hashCode() {
    return super.hashCode();
}

@Override
public String toString() {
    return super.toString();
}

@Override
public Object getProperty(String name) {
    for (PropertySource propertySource : originalPropertySource) {
        if (propertySource instanceof PropertySource) {
            continue;
        }
        if (propertySource.getClass().getName().contains("ConfigurationPropertySourcesPropertySource")) {
            continue;
        }
        if (propertySource.containsProperty(name)) {
            Object value = propertySource.getProperty(name);
            if (value instanceof String) {
                String str = value.toString();
                if (str.startsWith(PREFIX) && str.endsWith(SUFFIX)) {
                    int startIndex = 4;
                    int endIndex = str.length() - 1;
                    String encValue = str.substring(startIndex, endIndex);
                    return RSAUtils.decryptByPublic(encValue, RSAUtils.getPublicKey(RSAUtils.getPubKey()));
                }
            }


        }
    }
    return null;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值