BeanDefinitionRegistryPostProcessor忽略加密的属性
创始人
2024-11-27 03:00:21
0

可以在 BeanDefinitionRegistryPostProcessor 实现类中调用 PropertySourcesUtils 中的 resolvePlaceholders 方法,手动解析属性中的加密值。具体代码示例如下:

@Configuration public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    // Do nothing
}

@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
    StringEncryptor encryptor = new MyEncryptor(); // 自定义的加密类
    StringPropertyValueResolver resolver = new StringPropertyValueResolver() {
        @Override
        protected String resolveStringValue(String strVal) throws BeansException {
            if (strVal != null && strVal.startsWith("{cipher}")) {
                String cipherText = strVal.substring("{cipher}".length());
                return encryptor.decrypt(cipherText);
            }
            return strVal;
        }
    };
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addFirst(new MapPropertySource("myProps", new HashMap<>()));
    configurer.setPropertySources(propertySources);
    configurer.setPlaceholderPrefix("$(");
    configurer.setPlaceholderSuffix(")");
    configurer.setIgnoreUnresolvablePlaceholders(true);
    configurer.setValueSeparator(":");
    configurer.setSystemPropertiesMode(PropertySourcesPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE);
    configurer.setPropertyValues(new MutablePropertyValues())
    configurer.setPropertiesPersister(new DefaultPropertiesPersister());
    PropertySourcesUtils.addPlaceholders(propertySources, configurer);
    ResolvableType resolvableType = ResolvableType.forClass(StringValueResolver.class);
    registry.registerBeanDefinition("myResolver", BeanDefinitionBuilder.rootBeanDefinition(ResolverType.class)
            .addConstructorArgValue(resolver)
            .setFactoryMethod("resolvableAt", resolvableType)
            .getBeanDefinition());
}

}

其中,MyEncryptor 是自定义的加密类,实现了 StringEncryptor 接口。调用 resolver 的 resolveStringValue 方法可以手动解析属性中的加密值,将其解密后返回。在 propertySources 中添加自定义的

相关内容

热门资讯

wepok软件透明挂!wepo... 1、wepok软件透明挂!wepoke机制[辅助挂]原来真的有挂(有挂机制)-哔哩哔哩;代表性(透视...
wepok软件透明挂!wepo... wepok软件透明挂!wepoke软件透明挂测试[透视]其实真的有挂(有挂安装)-哔哩哔哩1、不需要...
AApoker透明挂!微扑克全... AApoker透明挂!微扑克全自动机器人[辅助挂]其实真的有挂(有挂软件)-哔哩哔哩1.微扑克 ai...
aapoker透明挂!微扑克代... aapoker透明挂!微扑克代打[透视]其实真的有挂(有挂APP)-哔哩哔哩是一款可以让一直输的玩家...
aapoker辅助!菠萝德州辅... aapoker辅助!菠萝德州辅助神器[辅助挂]原来真的有挂(有挂安装)-哔哩哔哩1、起透看视 菠萝德...
wepok软件透明挂!nzt德... 1、wepok软件透明挂!nzt德州辅助软件[辅助挂]其实真的有挂(有挂AI)-哔哩哔哩2、进入游戏...
aapoker透明挂!aapo... aapoker透明挂!aapoker发牌机制[辅助挂]原来真的有挂(有挂AI)-哔哩哔哩是一款可以让...
wpk透明挂!微扑克有挂[辅助... wpk透明挂!微扑克有挂[辅助挂]原来真的有挂(有挂AI)-哔哩哔哩;人气非常高,ai更新快且高清可...
aapoker辅助!wpk机器... aapoker辅助!wpk机器人[透视]其实真的有挂(有挂教程)-哔哩哔哩;致您一封信;亲爱aapo...
WPK透明挂!wpk俱乐部后台... WPK透明挂!wpk俱乐部后台管理系统[透视]原来真的有挂(有挂规律)-哔哩哔哩,您好,wpk俱乐部...