java项目配置文件加密

        最近有个项目要进行安全测试,对于数据库,redis,es等相关账号密码不能明文配置在配置文件中,因此想到了Jasypt 。

一、Jasypt 介绍

        Jasypt 是一个用于配置文件加密的 Java 库。它可以用来加密和解密配置文件中的敏感信息,如数据库密码、API 密钥等。

        主要分为两个版本:一个是1.9.2版本,一个是1.9.3版本。默认加密方式不一样。

  • 1.9.2版本:默认加密算法是PBEWithMD5AndDES
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=%content% password=%privateKey% algorithm=PBEWithMD5AndDES
  •  1.9.3版本:默认加密算法是PBEWITHHMACSHA512ANDAES_256
java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=%content% password=%privateKey% algorithm=PBEWITHHMACSHA512ANDAES_256 saltGeneratorClassName=org.jasypt.salt.RandomSaltGenerator ivGeneratorClassName=org.jasypt.iv.RandomIvGenerator providerName=SunJCE keyObtentionIterations=1000 stringOutputType=base64

二、Spring Boot 项目中使用 Jasypt

2.1 引入 Jasypt 依赖 在你的项目的构建文件中添加 Jasypt 依赖。在 pom.xml 文件中添加以下依赖:

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <!-- 2.x.x 版本对应 jasypt-1.9.2.jar; 3.x.x 对应 jasypt-1.9.3.jar -->
    <version>2.1.2</version>
</dependency>

2.2 创建加密密钥,需要创建一个加密密钥。

2.3 配置加密密钥在 Spring Boot 项目的配置文件中,添加以下属性配置:

jasypt:
  encryptor:
    password: your-password # 第2步设置的密码

2.4 配置文件加密 在配置文件中,将待加密的敏感信息用 ENC(加密后的内容) 包裹起来。例如,如果你要加密数据库密码,在配置文件中的配置项可以这样写:

db.password=ENC(加密后的密码)

项目启动时,就会获取到 ENC(加密后的密码)  类型的值,进行解密。

三、SpringBoot 加解密操作

import org.apache.commons.lang3.StringUtils;
import org.jasypt.encryption.StringEncryptor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Date 2024/1/19 15:20
 * @Desc jasypt加解密
 */
@RestController
@RequestMapping("/jasypt")
public class JasyptController {

    @Resource
    private StringEncryptor stringEncryptor;

    private final String PREFIX = "ENC(";
    private final String SUFFIX = ")";

    @GetMapping("/encrypt")
    public String testEncrypt(@RequestParam(value = "info") String info) {
        if (StringUtils.isEmpty(info)) {
            return AjaxResult.error("info不能为空");
        }
        // 数据加密
        String encryptStr = stringEncryptor.encrypt(info);
        return PREFIX + encryptStr + SUFFIX;
    }

    @GetMapping("/decrypt")
    public String testDecrypt(@RequestParam(value = "info") String info) {
        if (StringUtils.isEmpty(info)) {
            return AjaxResult.error("info不能为空");
        }
        // 数据解密
        String str = info;
        if (info.startsWith(PREFIX)) {
            String tmp = info.substring(PREFIX.length());
            str = tmp.substring(0, tmp.length() - SUFFIX.length());
        }
        return stringEncryptor.decrypt(str);
    }

}

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值