【springboot】 jasypt 密码加密

本文介绍了如何在SpringBoot项目中使用Jasypt库进行数据加密,包括引入依赖、配置加密盐值、数据库加密示例以及使用PBEWithMD5AndDES和AES_256算法的加密和解密方法。

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

1、引包

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

2、配置加密盐值,用来进行加密, 自定义

jasypt:
  encryptor:
    password: EbfYki

3、数据库加密、其他如redis等也可以

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/unite?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false&serverTimezone=Asia/Shanghai
    username: root
    #原密码123456
    password: ENC(t9cksZGhwkf2zXOMM7H5ug==)
    driver-class-name: com.mysql.cj.jdbc.Driver

4、加密工具



import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;

/**
 * Jasyp加解密工具类
 */
public class JasypUtils {

    private static final String PBEWITHMD5ANDDES = "PBEWithMD5AndDES";

    private static final String PBEWITHHMACSHA512ANDAES_256 = "PBEWITHHMACSHA512ANDAES_256";

    /**
     * Jasyp2.x 加密(PBEWithMD5AndDES)
     * @param		 value      待加密的原文
     * @param		 password         加密秘钥
     * @return       java.lang.String
     */
    public static String encryptWithMD5(String value, String password) {
        // 1. 创建加解密工具实例
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        // 2. 加解密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm(PBEWITHMD5ANDDES);
        config.setPassword(password);
        encryptor.setConfig(config);
        // 3. 加密
        return encryptor.encrypt(value);
    }

    /**
     * Jaspy2.x 解密(PBEWithMD5AndDES)
     * @param		 encryptedText      待解密密文
     * @param		 password             解密秘钥
     * @return       java.lang.String
     */
    public static String decryptWithMD5(String encryptedText, String password) {
        // 1. 创建加解密工具实例
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        // 2. 加解密配置
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        config.setAlgorithm(PBEWITHMD5ANDDES);
        config.setPassword(password);
        encryptor.setConfig(config);
        // 3. 解密
        return encryptor.decrypt(encryptedText);
    }

    public static void main(String[] args) throws  Exception{
        try {
            String value = "123456";
            String password = "EbfYki";
            String encryptWithMD5Str = encryptWithMD5(value, password);
            String decryptWithMD5Str = decryptWithMD5(encryptWithMD5Str, password);
            System.out.println("加密前原文:" + decryptWithMD5Str);
            System.out.println("加密后密文:" + "ENC(" + encryptWithMD5Str + ")");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}

Spring Boot是一个开源的Java框架,可帮助开发者更快速地构建应用程序。Jasypt3是Spring Boot中一种常用的加密库,可用于加密敏感数据,如数据库密码。下面简要介绍如何使用Spring BootJasypt3来加密数据库密码: 1. 添加依赖:在pom.xml中添加Jasypt3的依赖,如下所示: ```xml <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> ``` 2. 配置加密算法和密钥: 在Spring Boot的配置文件(application.properties或application.yml)中添加以下配置: ```properties jasypt.encryptor.algorithm=算法 jasypt.encryptor.password=密钥 ``` 3. 加密数据库密码: 在配置文件中使用Jasypt3提供的加密语法将数据库密码进行加密。例如,假设我们要加密密码是"password",可以使用以下语法: ```properties encryptor.encrypt(密码) ``` 4. 使用加密密码: 在项目中的数据源配置文件(如application.properties)中,使用加密后的密码。例如: ```properties spring.datasource.username=用户名 spring.datasource.password=ENC(加密密码) ``` 5. 运行应用程序: 启动Spring Boot应用程序,它将自动使用配置的密钥解密密码,然后使用解密后的密码连接数据库。 通过以上步骤,我们可以使用Spring BootJasypt3来实现数据库密码加密。这样可以保护敏感数据的安全性,同时提供了一种方便的方法来管理加密密钥和加密算法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值