Jasypt加密数据库配置信息

1. 背景

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

2. 前期准备

2.1. 依赖导入

本文使用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>

3. 生成密钥

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);
    }
}

4. 配置文件应用

此时我们拥有两个信息,一个为用于加密的密钥,一个为加密过后的密码。在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,查看获取到是否为解密后的密码!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值