1.EncryptPropertyPlaceholderConfigurer

自定义PropertyPlaceholderConfigurer继承PropertyPlaceholderConfigurer

package com.ruoyi.web;

import com.ruoyi.common.utils.StringUtils;
import org.omg.CORBA.Environment;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertiesPropertySource;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

/**
 * 配置解密,只能自定义一个PropertySourcesPlaceholderConfigurer,否则会报异常
 *
 * @author lyc
 **/
public class EncryptPropertyPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer implements InitializingBean {

    /**
     * 需要解密的配置项前缀
     */
    private static final String PREFIX_ENC = "enc:";

    private Environment environment;

    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Override
    protected Properties mergeProperties() throws IOException {
        Properties mergedProperties = new Properties();
        for (Properties localProp : localProperties) {
            mergedProperties.putAll(localProp);
        }

        for (Map.Entry entry : mergedProperties.entrySet()) {
            if (entry.getValue().toString().startsWith(PREFIX_ENC)) {
                String key = "0a9d3b9549ad7e8248433a76475cc128"; // 密钥
                String value = entry.getValue().toString().replace(PREFIX_ENC, StringUtils.EMPTY);
                mergedProperties.setProperty(entry.getKey().toString(), AesUtil.decode(value, key));
            }
        }

        //针对sharding-jdbc datasource自定义解密的特殊处理
        //因为sharding-jdbc的datasource注入是从environment中获取propertySource,
        //不能直接通过PropertySourcesPlaceholderConfigurer定义的datasource获取
//        MutablePropertySources sources = ((ConfigurableEnvironment) environment).getPropertySources();
//        sources.addFirst(new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergedProperties));

        return mergedProperties;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        localOverride = true;
    }
}

2.ResourcePlaceholderConfig

项目启动时加载application.yml

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

@Configuration
public class ResourcePlaceholderConfig {

    @Bean
    public PropertySourcesPlaceholderConfigurer propertyConfigurer() {
        PropertySourcesPlaceholderConfigurer config = new EncryptPropertyPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("application-druid.yml"));
        config.setProperties(yaml.getObject());
        return config;
    }
}

相关代码资料下载地址:https://download.csdn.net/download/DaLiShuiJiao/87542289

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐