【项目实战】在Spring Boot中使用Jasypt加密和解密敏感数据
一、背景说明
在服务中不可避免的需要使用到一些秘钥(数据库、redis等)开发和测试环境还好,但生产如果采用明文配置将会有安全问题,jasypt是一个通用的加解密库,可以使用它。
jasypt默认使用StringEncryptor来进行加解密,也可以自定义自己的加解密类来替换它 。
二、Jasypt介绍
2.1 Jasypt是什么?
Jasypt是一个Java库,Java 加密包,用于加密和解密敏感数据,例如密码和API密钥。
它允许开发者以最小的努力为他/她的项目添加基本的加密功能,而且不需要对密码学的工作原理有深刻的了解。
使用Jasypt对密码进行加解密可以保证密码的安全性。
2.2 Jasypt的功能特性
- 高安全性、基于标准的加密技术,既可用于单向加密也可用于双向加密。
- 加密密码、文本、数字、二进制文件…与Hibernate的透明集成。
- 适合集成到基于Spring的应用程序中,也可与Spring Security透明地集成。
- 对应用程序的配置(即数据源)进行加密的综合能力。
- 在多处理器/多核系统中具有高性能加密的特殊功能。
- 开放的API,可与任何JCE供应商一起使用。
- 提供了一组内置的加密和解密算法,用于保护敏感数据。
- 其中包括使用对称加密算法(如AES和Blowfish)和非对称加密算法(如RSA)进行加密和解密。
三、示例1:在Spring Boot中使用Jasypt加密和解密敏感数据
- 在Spring Boot中使用Jasypt加密和解密敏感数据非常简单。
- 您只需要在Spring Boot应用程序中添加Jasypt依赖项,并在应用程序配置文件中指定加密和解密密钥即可。
以下是在Spring Boot应用程序中使用Jasypt加密和解密密码的示例代码:
3.1 添加Jasypt依赖项
在Spring Boot应用程序的pom.xml文件中添加以下依赖项:
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
3.2 配置加密和解密密钥
在应用程序配置文件(例如application.properties或application.yml)中指定加密和解密密钥。
例如:
jasypt.encryptor.password=mySecretKey
3.3 在应用程序中使用加密和解密功能
在应用程序中使用Jasypt加密和解密密码非常简单。
您只需要使用@Autowired注释注入org.jasypt.encryption.StringEncryptor bean,
并使用encrypt和decrypt方法加密和解密密码。
例如:
import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MyService {
@Autowired
private StringEncryptor encryptor;
public void doSomething() {
String password = "myPassword";
String encryptedPassword = encryptor.encrypt(password);
String decryptedPassword = encryptor.decrypt(encryptedPassword);
System.out.println("Original password: " + password);
System.out.println("Encrypted password: " + encryptedPassword);
System.out.println("Decrypted password: " + decryptedPassword);
}
}
四、示例2:在Spring Boot中使用Jasypt加解密密码的步骤
以下是在Spring Boot中使用Jasypt加解密密码的步骤:
4.1 在pom.xml文件中添加依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
4.2 在application.properties或application.yml中配置加密算法和密钥:
jasypt.encryptor.algorithm=PBEWithMD5AndDES
jasypt.encryptor.password=your_secret_password
或者:
jasypt:
encryptor:
algorithm: PBEWithMD5AndDES
password: your_secret_password
4.3 在代码中使用Jasypt对密码进行加解密:
import org.jasypt.util.password.StrongPasswordEncryptor;
public class PasswordUtils {
private static final StrongPasswordEncryptor PASSWORD_ENCRYPTOR = new StrongPasswordEncryptor();
public static String encrypt(String password) {
return PASSWORD_ENCRYPTOR.encryptPassword(password);
}
public static boolean checkPassword(String plainPassword, String encryptedPassword) {
return PASSWORD_ENCRYPTOR.checkPassword(plainPassword, encryptedPassword);
}
}
其中,encrypt方法用于加密密码,checkPassword方法用于检查明文密码与密文密码是否匹配。
4.4 在代码中使用加密后的密码:
String encryptedPassword = PasswordUtils.encrypt("password");
userRepository.save(new User("username", encryptedPassword));
4.5 在application.properties或application.yml中使用加密后的密码:
spring.datasource.password=ENC(encrypted_password)
或者:
spring:
datasource:
password: ENC(encrypted_password)
其中,encrypted_password是使用Jasypt加密后的密码。
4.6 启动应用程序时,Spring Boot会自动解密该密码并将其用于数据库连接。
五、参考地址:
https://www.cnblogs.com/yangzhilong/p/10881589.html
https://www.jianshu.com/p/7bf1b2c01164
https://github.com/ulisesbocchio/jasypt-spring-boot
相关文章
- 玩转 Spring Boot 集成篇(@Scheduled、静态、动态定时任务)(七)
- Spring 全家桶之 Spring Boot 2.6.4(七)- Exception
- Spring Boot 最新发布,支持 Java 19,这速度太快了。。
- 编写一个spring-boot-starter-fastjson
- Spring Boot + minio 实现高性能存储服务,So Easy~!
- 解决Spring Boot项目放到服务器上运行出现时差问题
- Github标星35K+超火的Spring Boot实战项目,附超全教程文档
- Java项目分享-适合 Spring Boot 初学者的 OA 开源项目
- Java项目分享-基于Spring Boot的迷你天猫商城
- Spring boot项目集成security
- spring boot docker部署_docker部署java项目
- Spring Boot 项目鉴权的 4 种方式
- SpringBoot基础学习文章-Java环境变量配置Maven介绍Spring Boot介绍
- spring boot整合pagehelper分页插件
- 【经验】在Idea中怎么一个spring boot服务怎么模拟多台实例部署
- 这款Spring Boot + Vue开源博客系统实战项目太赞了!
- 全面学习MongoDB,在Spring Boot项目中整合 MongoDB
- Spring Boot 2.4.3、2.3.9 版本发布,你准备好了吗?
- Spring Boot的核心注解和配置(一)
- Spring Boot 实现扫码登录,这种方式太香了!!
- Spring Boot 项目集成Redis的方式详解
- Spring-Boot快速搭建web项目详细总结详解编程语言
- Spring Boot读取配置的 5 种方式详解编程语言
- Spring Boot(十三):spring boot小技巧详解编程语言
- spring boot项目:java -jar命令 没有主清单属性详解编程语言
- Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase