Spring项目Application.xml配置文件加解密(免依赖添加)

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以使用加密算法来加密配置文件,以保护敏感信息的安全性。以下是一种常见的加密配置文件的方法: 1. 导入依赖:在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-crypto</artifactId> </dependency> ``` 2. 创建一个加密/解密工具类:可以使用`TextEncryptor`接口提供的实现类来进行加密和解密操作。例如,可以使用`Jasypt`库提供的实现。 3. 配置加密密钥:在`application.properties`或`application.yml`配置文件中,添加一个密钥属性。例如: ```properties jasypt.encryptor.password=mySecretKey ``` 4. 加密配置信息:可以使用上一步中配置的密钥来加密敏感信息。例如,可以在配置文件中使用`ENC(encryptedValue)`的形式来表示加密后的值。例如: ```properties my.property=ENC(encryptedValue) ``` 5. 解密配置信息:在需要访问敏感信息的地方,可以使用加密/解密工具类来解密配置值。例如,在Spring Boot应用程序中,可以使用`@Value`注解将解密后的值注入到属性中。 ```java @Value("${my.property}") private String myProperty; ``` 这样,敏感信息就会在配置文件中以加密形式存储,并且在应用程序中自动解密以供使用。 请注意,为了确保密钥的安全性,建议将密钥存储在安全的位置,并且不要将明文密钥直接硬编码在代码中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值