SpringBoot整合jasypt,实现配置文件密码自动加解密

1. 引入依赖

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

2. 自定义加密器和注入

自定义加密器

package com.lt.javastu.jasypt;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

/**
 * 自定义加密器
 */
public class MyStringEncryptor implements StringEncryptor {

    /**
     * 加解密算法
     */
    public static final String PBE_ALGORITHMS_MD5_DES = "PBEWITHMD5ANDDES";
    public static final String PBE_ALGORITHMS_MD5_TRIPLEDES = "PBEWITHMD5ANDTRIPLEDES";
    public static final String PBE_ALGORITHMS_SHA1_DESEDE = "PBEWITHSHA1ANDDESEDE";
    public static final String PBE_ALGORITHMS_SHA1_RC2_40 = "PBEWITHSHA1ANDRC2_40";

    /**
     * 加解密盐值
     */
    private String salt;

    /**
     * 加解密算法
     */
    private String algorithm = PBE_ALGORITHMS_MD5_DES;

    public MyStringEncryptor(String salt) {
        this.salt = salt;
    }

    public MyStringEncryptor(String password, String algorithm) {
        this.salt = password;
        this.algorithm = algorithm;
    }

    /**
     * 加密
     * @param message
     * @return
     */
    @Override
    public String encrypt(String message) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        // 加解密盐值
        encryptor.setConfig(getConfig(this.salt));
        return encryptor.encrypt(message);
    }

    /**
     * 解密
     * @param encryptedMessage
     * @return
     */
    @Override
    public String decrypt(String encryptedMessage) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(getConfig(this.salt));
        return encryptor.decrypt(encryptedMessage);
    }

    /**
     * 获取配置
     * @param salt 加密盐值
     * @return
     */
    public SimpleStringPBEConfig getConfig(String salt) {
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        // 加密盐值
        config.setPassword(salt);
        // 加解密算法
        config.setAlgorithm(PBE_ALGORITHMS_MD5_DES);
        // 设置密钥获取迭代次数
        config.setKeyObtentionIterations(1000);
        // 线程池大小:默认1
        config.setPoolSize(1);
        // 盐值生成器className
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        // 设置字符串输出类型
        config.setStringOutputType("base64");
        return config;
    }

	/**
     * 测试
     * @param args
     */
    public static void main(String[] args) {
        MyStringEncryptor encryptor = new MyStringEncryptor("ltpassword");
        String encrypt = encryptor.encrypt("root666*");
        System.out.println(encrypt);
        String decrypt = encryptor.decrypt(encrypt);
        System.out.println(decrypt);
    }
}

注入

package com.lt.javastu.jasypt;

import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JasyptConfig {
    /**
     * 加解密盐值
     */
    @Value("${jasypt.encryptor.password}")
    private String password;

    @Bean("myStringEncryptor")
    public StringEncryptor myStringEncryptor() {
        return new MyStringEncryptor(password);
    }
}

3. 配置文件示例

spring.datasource.password=ENC(z97PjQoxAU9WeA3qSMkLrk4OmiDEyPSB)

# 替换成自己的盐
jasypt.encryptor.password=ltpassword
jasypt.encryptor.bean=myStringEncryptor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Lt0_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值