SpringBoot集成Jasypt实现敏感信息加密

本文介绍了如何在SpringBoot应用中集成Jasypt库来加密和解密数据库、Redis等敏感信息,包括添加依赖、配置加密密钥、代码示例以及生成和使用加密后的密码。
摘要由CSDN通过智能技术生成

项目场景:

        在服务中不可避免的需要使用到一些秘钥(数据库、redis等)开发和测试环境还好,但生产如果采用明文配置将会有安全问题,jasypt是一个通用的加解密库,可以使用它。

        在Spring Boot中使用Jasypt加密和解密敏感数据非常简单,只需要在Spring Boot应用程序中添加Jasypt依赖项,并在应用程序配置文件中指定加密和解密密钥即可。


实现步骤:

1、添加Jasypt依赖

<!-- 配置文件敏感信息加密 -->
<dependency>
        <groupId>com.github.ulisesbocchio</groupId>
        <artifactId>jasypt-spring-boot-starter</artifactId>
        <version>2.1.1</version>
</dependency>

2、添加配置信息 

为了避免密钥泄露,密钥不应该直接放在文件中,这里为了方便暂时放在配置文件中。

# 本地启动配置文件,加密秘钥需在该配置中指定,便于开发交流
# 正式环境可配置JVM启动参数: -Djasypt.encryptor.password=testjasypt
jasypt.encryptor.password = testjasypt
#加密算法,不配置使用默认的
#jasypt.encryptor.algorithm = PBEWithMD5AndDES       

正式环境可以把秘钥放在环境变量中启动 

java -Djasypt.encryptor.password=秘钥 -jar xxx.jar 

3、生成密文

方式一:java文件生成加密

package com.test.controller;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.util.text.BasicTextEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * jasypt加密解密
 */
@Api(tags = "jasypt加密解密测试")
@RestController
@RequestMapping("/v1/jasypt")
public class JasyptController {

    @Autowired
    private StringEncryptor stringEncryptor;

    /**
     * jasypt加密
     *
     * @param password
     */
    @ApiOperation(value = "jasypt加密")
    @GetMapping("/encrypt")
    public String encrypt(String password) {
        return stringEncryptor.encrypt(password);
    }

    /**
     * jasypt解密
     *
     * @param password
     * @return
     */
    @ApiOperation(value = "jasypt解密")
    @GetMapping("/decrypt")
    public String decrypt(String password) {
        return stringEncryptor.decrypt(password);
    }
    
    public static void main(String[] args) {
		BasicTextEncryptor encryptor = new BasicTextEncryptor();
		//加密密钥
		encryptor.setPassword("testjasypt");
    	//加密
		String encData = encryptor.encrypt("test123456!");
		//解密
		String decData = encryptor.decrypt(encData);
		System.out.println("加密后:"+encData);
		System.out.println("解密后:"+decData);
	}
}

方式二:CMD命令行加密

通过该种方式获取密文,需要到maven仓库下jasypt-1.9.2.jar(版本根据自身选择)包所在的路径下执行,否则会报找不到对应的主类,一般目录在repository\org\jasypt\jasypt\下面。

// 加密命令
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input='test123456!' password=testjasypt algorithm=PBEWithMD5AndDES

// 解密命令
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input='Vqlju4F/mF4dIxGfxk2LMcNVx8NTwbln' password=testjasypt algorithm=PBEWithMD5AndDES

参数说明:

  • org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI是jasypt提供的一个用于加密的实体类
  • input表示需要加密的字符串如:密码
  • password表示本次加密算法使用的秘钥
  • algorithm表示加密算法的名称,默认PBEWithMD5AndDES

 

 

4、配置加密后的密码

ENC()是固定格式,括号里是上一步生成的密文。

#原来的密码
#spring.datasource.password=test123456!
#加密后的密码
spring.datasource.password=ENC(Vqlju4F/mF4dIxGfxk2LMcNVx8NTwbln)

源码地址:https://download.csdn.net/download/u011974797/88985378 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

涛哥是个大帅比

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值