SpringBoot配置文件加密

本文介绍了如何在Spring Boot应用中使用Jasypt进行密码加密,包括配置秘钥、加密过程和将加密后的密码应用到数据库。重点讲解了加密字符串的生成和应用到application.properties中的步骤。
摘要由CSDN通过智能技术生成

 

一、使用jasypt加密

 

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

1、增加配置文件jasypt.encryptor.password = AARON@2020,这是加密的秘钥;
2、所有明文密码替换为ENC(加密字符串),例如ENC(XW2daxuaTftQ+F2iYPQu0g==);

第三步的加密字符串的生成方式为:
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=Afei@2018 algorithm=PBEWithMD5AndDES

其中:

input的值就是原密码。
password的值就是参数jasypt.encryptor.password指定的值,即秘钥。

 

        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt(盐)
        textEncryptor.setPassword("G0CvDz7oJn6");
        //要加密的数据(数据库的用户名或密码)
        String username = textEncryptor.encrypt("root");
        String password = textEncryptor.encrypt("root123");
        System.out.println("username:"+username);
        System.out.println("password:"+password);

将生成的加密串配置ENC(加密串)到application.properties中

 

# 加密所需的salt(盐)
jasypt.encryptor.password=G0CvDz7oJn6
# 默认加密方式PBEWithMD5AndDES,可以更改为PBEWithMD5AndTripleDES
# jasypt.encryptor.algorithm=PBEWithMD5AndDES
spring.datasource.username=ENC(6eaMh/RX5oXUVca9ignvtg==)
spring.datasource.password=ENC(6eaMh/RX5oXUVca9ignvtg==)

部署时配置salt(盐)值

java -jar -Djasypt.encryptor.password=G0CvDz7oJn6 xxx.jar

 

 

转自https://www.jianshu.com/p/fd759aea24d0



作者:Binary_r
链接:https://www.jianshu.com/p/fd759aea24d0
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值