SpringBoot使用jasypt加解密密码的实现方法_代码知识_七洗推广网

SpringBoot使用jasypt加解密密码的实现方法

#代码知识 发布时间: 2026-01-12

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./article/197600.htm)

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);
 }
}
代码知识SEO

上一篇 : Linux 中shell脚本设置开头固定格式的实现方法

下一篇 : Comet框架Plushlet使用问题解决方案
品牌营销
专业SEO优化
添加左侧专家微信
获取产品详细报价方案