springboot使用jasypt对配置文件加密,加密数据库连接

依赖

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>1.14</version>
</dependency>

springboot配置

yml配置

jasypt:
  encryptor:
    password: saltValue    #salt值,密文加盐
spring:
  datasource: # 数据库链接
    db1:
      jdbc-url: jdbc:mysql://x.x.x.x:3306/db_test?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
      username: root      #也可以加密用户名,依然是ENC()格式,这里没有进行加密
      password: ENC(OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+)  #加密了密码,ENC()括号内为密文
      driver-class-name: com.mysql.cj.jdbc.Driver
      mapper-locations: classpath*:mapper/otcmapper/*.xml

启动类添加注解:@EnableEncryptableProperties

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEncryptableProperties
@EnableScheduling
//@EnableAsync
public class SpBatchApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpBatchApplication.class, args);
    }
}

通过明文获取加密的值

cmd在自己的maven仓库目录下执行命令,(要保证依赖下载下来了)
解释:
input:文字的明文
password:加密的盐值(可随意,必须=jasypt:encryptor:password: saltValue)
algorithm:PBEWithMD5AndDES(默认算法)

java -cp org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="密码明文" password=saltValue algorithm=PBEWithMD5AndDES

执行后输出结果:OUTPUT就是密文了,把密文替换yml的属性值就行
ENC(OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+)

----ARGUMENTS-------------------

algorithm: PBEWithMD5AndDES
input: 密码明文
password: saltValue



----OUTPUT----------------------

OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+

启动springboot就会自动解密了

通过密文和盐值解密得到明文

java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="加密后的密文" password=saltValue algorithm=PBEWithMD5AndDES
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

可——叹——落叶飘零

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

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

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

打赏作者

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

抵扣说明:

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

余额充值