SpringBoot 加密yml配置文件

前言
对于SpringBoot项目,项目中依赖的配置,例如数据库的配置信息,默认都是明文保存在配置文件中。这种方式存在密码泄露风险,在企业的安全扫描中也通过不了,所以需要对配置进行加密处理。今天介绍一种可以快速集成到SpringBoot的加解密库Jasypt。

1、pom引入依赖

        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot</artifactId>
            <version>1.18</version>
        </dependency>
        <dependency>
            <groupId>org.jasypt</groupId>
            <artifactId>jasypt</artifactId>
            <version>1.9.2</version>
        </dependency>

2、在yml文件中添加jasypt配置

# 配置文件密码加密配置
jasypt:
  encryptor:
    password: EbfYkitulv73I2p0mXI50JMXoaxZTKJ7 # 可以修改成自己的秘钥
    algorithm: PBEWithMD5AndDES # 加密算法
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

3、获取密文
在终端输入以下命令
input:要加密的密码
password:秘钥
algorithm:加密算法

java -cp /Users/dhjie/Desktop/software/apache-maven-3.6.3/repository/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="adad@12323" password=sdffdn@12345 algorithm=PBEWithMD5AndDES

解密

java -cp /Users/dhjie/Desktop/software/apache-maven-3.6.3/repository/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="rbjwAgN5e032Z4YYmtdxk4j86K7kfdsdhjs" password=sdffdn@12345 algorithm=PBEWithMD5AndDES

4、修改原来的密码
原来的明文密码值,改为 ENC(xxx) ,其中xxx是密文。
列如:

5、添加注解
启动类上添加@EnableEncryptableProperties

补充

也可以把秘钥从yml中删除掉,加入到启动命令中即可
idea启动:
在这里插入图片描述
命令启动:
java -Djasypt.encryptor.password=dad@23232 -jar

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值