springboot整合jasypt实现配置文件加密

springboot整合,用于加密配置文件密码

简介:

在配置文件中,往往会有密码,例如mysql,redis等。如果直接将密码以明文的形式写在项目中,就会有泄露的风险。
所以这里使用jasypt,用于配置文件加密。

应用方式

1、导入依赖
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.4</version>
</dependency>

2、工具类
package com.walker.tools.utils;

import lombok.extern.slf4j.Slf4j;
import org.jasypt.properties.PropertyValueEncryptionUtils;
import org.jasypt.util.text.BasicTextEncryptor;

@Slf4j
public final class JasyptUtils {

    public static void main(String[] args) {
        String a = encrypt("aaaa");
        decrypt(a);

    }
    /**
     * 加密使用密钥
     */
    private static final String PRIVATE_KEY = "helloworldforyou";

    private static BasicTextEncryptor basicTextEncryptor = new BasicTextEncryptor();

    static {
        basicTextEncryptor.setPassword(PRIVATE_KEY);
    }

    /**
     * 私有构造方法,防止被意外实例化
     */
    private JasyptUtils() {
    }

    /**
     * 明文加密
     *
     * @param plaintext 明文
     * @return String
     */
    public static String encrypt(String plaintext) {
        log.info("明文字符串为:{}", plaintext);
        // 使用的加密算法参考2.2节内容,也可以在源码的类注释中看到
        String ciphertext = basicTextEncryptor.encrypt(plaintext);
        log.info("密文字符串为:{}", ciphertext);
        return ciphertext;
    }

    /**
     * 解密
     *
     * @param ciphertext 密文
     * @return String
     */
    public static String decrypt(String ciphertext) {
        log.info("密文字符串为:{}", ciphertext);
        ciphertext = "ENC(" + ciphertext + ")";
        if (PropertyValueEncryptionUtils.isEncryptedValue(ciphertext)) {
            String plaintext = PropertyValueEncryptionUtils.decrypt(ciphertext, basicTextEncryptor);
            log.info("明文字符串为:{}", plaintext);
            return plaintext;
        }
        log.error("解密失败!");
        return "";
    }
}

3、配置文件 application.yaml
jasypt:
  encryptor:
    # 指定加密密钥,生产环境请放到启动参数里面
    password: helloworldforyou
    # 指定解密算法,需要和加密时使用的算法一致
    algorithm: PBEWithMD5AndDES
    # 指定initialization vector类型
    iv-generator-classname: org.jasypt.iv.NoIvGenerator


test:
  entry: ENC(KCJWcMKAxNct3H3AN41/Zw==)

algorithm:算法
可以进行配置,例如MD5等
在配置文件中,可以使用ENC(加密内容)去配置变量,达到加密的效果。防止配置文件直接被获取,起到保护的作用

4、测试

1、使用相同的加密key,对字符串进行加密,然后将字符串赋予到配置文件中
image.png

image.png

2、编写一个测试类

package com.walker.tools;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class WalkerToolsApplicationTests {

    @Value("${test.entry}")
    private String value;

    @Test
    void contextLoads() {
        System.out.println(value);
    }

}

3、执行之后,就能够获取解密后的结果
image.png

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

WalkerShen

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

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

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

打赏作者

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

抵扣说明:

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

余额充值