【Spring】@PropertySource 配置文件自定义加密、自定义Yaml文件加载

前言

今天研究了一下关于配置文件的自定义加密,很久以前用过 jasypt 加密,依稀记得运行 jar 包的时候还得把密码写到执行命令里面,不大方便,然后碰巧看到有使用自定义 Yaml 文件的方式进行加密解密的,就试着玩了一下,这篇博客简单介绍一下用法,除了加密解密以外还有简单介绍一下配置文件的读取替换。

参考目录

实现步骤

1、包结构

在这里插入图片描述

2、Maven

除了常见的配置以外,需要引入的是 Hutoolbcprov-jdk15to18,前者是加密工具类,后者是加密所需要的 jar。

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.16</version>
</dependency>

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15to18</artifactId>
    <version>1.69</version>
</dependency>

除此之外,需要注意 <build> 标签下的配置,Maven 打包时会把 resources 文件夹下相应环境的文件替换到 properties 下。

<resource>
    <directory>${project.basedir}/src/main/resources/profile/${profiles.active}</directory>
    <filtering>true</filtering>
    <includes>
        <include>*.*</include>
    </includes>
    <targetPath>properties</targetPath>
</resource>

完整的代码可以看代码包。

3、自定义配置文件

dev 为例。

encrypt.yml
在这里插入图片描述

business.properties
在这里插入图片描述

4、application 文件

application.yml
在这里插入图片描述

application-dev.yml
在这里插入图片描述

5、自定义数据库配置 MyDataSource

在这里插入图片描述
这里使用了工具类对配置文件的字符串解密之后再保存到 Hikari 配置中。

6、加密配置 EncryptYamlProperties

在这里插入图片描述
@PropertySource 指定了引用的配置文件,以及读取配置的方法是 MyPropertySourceFactory,因为不能直接读取 Yaml 文件配置,所以需要转换成 Properties

7、自定义读取yaml配置 MyPropertySourceFactory

在这里插入图片描述

8、测试加密解密

启动项目,如果正常启动就说明数据库连接正常,保险起见,引入了一个TestDemo 的类测试,也引入了 MyBatis-Plus 简化代码的书写。

在这里插入图片描述

请求成功:
在这里插入图片描述

9、自定义 Properties 文件读取

/**
 * 业务yaml文件配置
 *
 * @author Michelle.Chung
 */
@Component
public class BusinessProperties {

    /**
     * 文件路径
     */
    private static final String BUSINESS_PROPERTIES = "properties/business.properties";

    /**
     * 单例
     */
    private static final BusinessProperties INSTANCE = new BusinessProperties();

    private final Properties properties;

    public BusinessProperties() {
        try {
            this.properties = PropertiesLoaderUtils.loadAllProperties(BUSINESS_PROPERTIES);
        } catch (IOException e) {
            throw new RuntimeException("读取配置文件时出现异常...", e);
        }
    }

    /**
     * 获得当前实例
     *
     * @return
     */
    public static BusinessProperties getInstance() {
        return INSTANCE;
    }

    /**
     * 获取value值
     */
    public String getValue(String key) {
        if (null == properties || StrUtil.isBlank(key)) {
            return null;
        }
        return properties.getProperty(key);
    }

}

10、测试自定义配置读取

在这里插入图片描述

请求成功:

在这里插入图片描述

最后说几句

本文比较简单,我觉得这个功能用不用见仁见智吧,开始也只是看到觉得没有用过所以测试一下,也方便以后如果有机会的话可以参考。

(完)

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
`@PropertySource`注解默认只支持`.properties`格式的配置文件,不支持`.yaml`和`.yml`格式的配置文件。如果需要读取`.yaml`或`.yml`格式的配置文件,可以使用`@ConfigurationProperties`注解来读取。 假设我们有一个`application.yml`配置文件,在其中定义了一个`my.package`变量,我们可以按照以下方式在`RestControllerAdvice`中使用它: 1. 在`application.yml`中定义变量: ```yaml my: package: com.example.myapp ``` 2. 在`RestControllerAdvice`中使用`@ConfigurationProperties`注解读取配置文件中的变量: ```java @ConfigurationProperties(prefix = "my") public class MyProperties { private String package; public String getPackage() { return package; } public void setPackage(String packageName) { this.package = packageName; } } @RestControllerAdvice(basePackages = "#{@myProperties.package}") public class MyRestControllerAdvice { // ... } ``` 在这个例子中,`@ConfigurationProperties`注解用于读取`application.yml`中的`my.package`属性,并将其映射为`MyProperties`对象的`package`属性。然后,我们可以使用SpEL表达式`#{@myProperties.package}`来引用这个属性,从而指定了`MyRestControllerAdvice`所要扫描的包。需要注意的是,`@ConfigurationProperties`注解需要在Spring配置类中使用,并且要将`MyProperties`对象注册到Spring容器中。 这样,就可以使用`@ConfigurationProperties`注解来读取`.yaml`和`.yml`格式的配置文件,并将其中的变量传递给`basePackages`属性了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MichelleChung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值