Jasypt加密数据库配置信息

背景

在配置文件中配置公网数据库时需要对认证信息进行加密,以此来保障系统安全性,可以通过Jasypt来进行加密。

前期准备

依赖导入

本文使用Maven进行依赖导入,在pom.xml中添加依赖

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

需要注意的是,请根据Java版本来选择适合的 Jasypt 版本使用的是 JDK 8,则是对应 2.x,而对于 JDK 9 及以上版本,则应选择 3.x 版本。

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

生成密钥

Jasypt通过密钥来对明文密码进行加密,所以需要提供一个密钥,该密钥需为ASSIC码。

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.junit.Test;

public class JasyptTest {
    // 加密的密钥,必须为ASCll码
    private String privateKey = "这里存放自定义的密钥";
    //生成加密后的密码
    @Test
    public void testEncrypt() throws Exception {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        // 加密的算法,这个算法是默认的
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPassword(privateKey);                      
        standardPBEStringEncryptor.setConfig(config);
        String plainText = "准备加密的明文密码";
        String encryptedText = standardPBEStringEncryptor.encrypt(plainText);
        System.out.println("用于加密的密钥: " + privateKey);
        System.out.println("加密后的密码: " + encryptedText);
    }
    //解密测试
    @Test
    public void testDe() throws Exception {
        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPassword(privateKey);  
        standardPBEStringEncryptor.setConfig(config);
        String encryptedText = "填写已加密的密码";
        String plainText = standardPBEStringEncryptor.decrypt(encryptedText);
        System.out.println("用于加密的密钥: " + privateKey);
        System.out.println("用于解密的密码: " + encryptedText);
        System.out.println("解密后的密码:" + plainText);
    }
}

配置文件应用

此时我们拥有两个信息,一个为**用于加密的密钥**,一个为**加密过后的密码**。在application.yml中添加如下配置:

在数据库配置那里密码必须为ENC()包裹

jasypt:
  encryptor:
    password: 加密的密钥
    # 注意:官方说明中只要求password必填,但后面两项也要填上,否则出现错误
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

# 示例
spring:
  datasource:
    password: ENC(加密过后的密码)

定义测试接口,查看是否成功获取到解密后的密码

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/test")
public class TestController {
    @Value("${spring.datasource.password}")
    private String decryptedPassword;

    @GetMapping("/password")
    public String hello(){
        return "Decrypted Password: " + decryptedPassword;
    }
}

最后访问项目地址/test/password,查看获取到是否为解密后的密码!

posted @ 2025-06-13 22:59  TttRark  阅读(16)  评论(0)    收藏  举报