SpringBoot2集成jasypt3.0.3 最简便配置文件 数据库密码 加密工具(加密解密工具类,非命令行)

环境
  • SpringBoot2.0以上
  • jasypt 3.0.3
  • jdk8
加密
username: root     # 数据库账号
password: 123456     # 数据库密码
  1. 导入依赖
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>

  1. application.yml中添加如下配置

注意:密钥可以写在application.yml配置文件中,也可以直接加入到idea的虚拟机参数中

在这里插入图片描述

jasypt:
  encryptor:
    # 密钥
    password: jasypt
    # 指定加密方式
    algorithm: PBEWithMD5AndDES
    iv-generator-classname: org.jasypt.iv.NoIvGenerator

  1. 创建密码加密工具类

注意:代码中“jasypt”为jasypt所需要的加密密码配置,也就是秘钥,可以替换成自己需要的

public class JasyptUtil {
    
    
    public static void main(String[] args) {
    
    
        // 加密
        String encPwd1 = encyptPwd("jasypt", "root");
        // 加密
        String encPwd2 = encyptPwd("jasypt", "123456");
        System.out.println(encPwd1);
        System.out.println(encPwd2);
    }
    /**
     * 加密方法
     * @param password jasypt所需要的加密密码配置
     * @param value    需要加密的密码
     */
    public static String encyptPwd(String password, String value) {
    
    
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        String result = encryptor.encrypt(value);
        return result;
    }
}
  1. 运行工具
    在这里插入图片描述

  2. 替换application.yml中的账号密码

注意:生成的加密数据放到配置文件时需要用ENC()包围起来

username: ENC(3g2WGnuz+u7Fq1T6xbQ7TQ==)     # 数据库账号
password: ENC(zniE4Hlid104xsxdzQcK/w==)     # 数据库密码
解密
  1. 重复加密步骤1
  2. 重复加密步骤2
  3. 创建解密工具类
public static void main(String[] args) {
    
    
        // 加密
        String encPwd1 = decyptPwd("jasypt", "3g2WGnuz+u7Fq1T6xbQ7TQ==");
        // 加密
        String encPwd2 = decyptPwd("jasypt", "zniE4Hlid104xsxdzQcK/w==");
        System.out.println("username"+encPwd1);
        System.out.println("password"+encPwd2);
    }
    
    /**
     * 解密
     * @param password jasypt所需要的加密密码配置
     * @param value    需要解密的密码
     */
    public static String decyptPwd(String password, String value) {
    
    
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        String result = encryptor.decrypt(value);
        return result;
    }
  1. 运行工具

在这里插入图片描述

总结

完整工具代码

public class JasyptUtil {
    
    
    public static void main(String[] args) {
    
    
        // 加密
        String encPwd1 = encyptPwd("jasypt", "root");
        // 加密
        String encPwd2 = encyptPwd("jasypt", "2190268123asd");
        // 解密
        String decPwd1 = decyptPwd("jasypt", encPwd1);
        // 解密
        String decPwd2 = decyptPwd("jasypt", encPwd2);
        System.out.println(encPwd1);
        System.out.println(encPwd2);
        System.out.println("username:"+decPwd1);
        System.out.println("password:"+decPwd2);
    }

    /**
     * 解密
     * @param password jasypt所需要的加密密码配置
     * @param value    需要加密的密码
     */
    public static String decyptPwd(String password, String value) {
    
    
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        String result = encryptor.decrypt(value);
        return result;
    }
    /**
     * 加密方法
     * @param password jasypt所需要的加密密码配置
     * @param value    需要加密的密码
     */
    public static String encyptPwd(String password, String value) {
    
    
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        String result = encryptor.encrypt(value);
        return result;
    }

}

jasypt3.0.3的加密解密是非常简单的,大家可以继续封装成一个工具,把重复代码提出来,可以做到不仅仅是加密配置文件,也可以加密包括一些数据。

另外jasypt3.0.3的版本中,每次加密相同的数据组得到的加密后密码是不同的,无论取哪个密码,都不影响使用哦!

如果觉得有帮助的话给个免费的点赞吧,Thanks♪(・ω・)ノ

猜你喜欢

转载自blog.csdn.net/jxysgzs/article/details/110056483