SpringBoot 使用 Jasypt 加解密

本文介绍了如何在Spring Boot应用中使用jasypt进行数据加密,包括引入依赖、配置盐值、加密解密示例以及将加密后的密码存储在配置文件中。特别强调了通过启动参数传递盐值以增强安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 引入依赖
<dependency>
	<groupId>com.github.ulisesbocchio</groupId>
	<artifactId>jasypt-spring-boot-starter</artifactId>
	<version>2.1.0</version>
</dependency>
  1. 在属性文件中配置盐
jasypt:
  encryptor:
    password: salt
  1. 在测试类中获取加密后的属性
public static String encryptJasypt(String salt, String key) {
	BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
	textEncryptor.setPassword(salt);
	return textEncryptor.encrypt(key);
}

public static String decryptJasypt(String salt, String key) {
	BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
	textEncryptor.setPassword(salt);
	return textEncryptor.decrypt(key);
}

public static void main(String[] args) {
	String s = encryptJasypt("salt", "mypass");
	System.out.println(s);
}
aQh9vhMxqfZZNGZouh0VHA==
  1. 在配置文件中设置加密后的属性(完成)
spring:
  datasource:
    password: ENC(aQh9vhMxqfZZNGZouh0VHA==)
  1. 补充:将盐值作为项目启动时的参数传入,而不是写在配置文件里,才能真正提高安全性。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值