SpringBoot使用jasypt加解密密码

jasypt是一个通用的加解密库,我们可以使用它在配置文件中对数据库密码进行加密,以确保其安全性。

1、注入依赖

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

2、配置文件

#以数据库密码加密为例
## 数据源配置
spring.datasource.url=jdbc:mysql://lochost:3306/jasypt?characterEncoding=utf8
spring.datasource.username=root
#Fddt+VfcW5+j5lAbuOXxPB3mGb0iBLLe 是采用jasypt进行加密以后生成的密文
spring.datasource.password=ENC(Fddt+VfcW5+j5lAbuOXxPB3mGb0iBLLe)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#jasypt加密的密匙
jasypt.encryptor.password=abcderf(这个是自己设置的)

那么如何得到这个密文呢?
1、win+r cmd打开命令窗口 在你的maven库中找到 jasypt-1.9.2.jar 包
执行下面的命令

java -cp D:\Maven\repository\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="密钥(abcderf)" password=root(加密的密码) algorithm=PBEWithMD5AndDES

然后复制密文即可
在这里插入图片描述
2、代码生成(这种方法没有使用过 参考链接:https://www.cnblogs.com/yangzhilong/p/10881589.html

import org.jasypt.util.text.BasicTextEncryptor;

public class Test {
	public static void main(String[] args) {
	
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt(盐)
        textEncryptor.setPassword("PBEWithMD5AndDES");
        //要加密的数据(数据库的用户名或密码)
        String username = textEncryptor.encrypt("root");
        String password = textEncryptor.encrypt("root");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
    }
}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值