美文网首页
Spring Boot 集成 jasypt 3.0.3 配置文件

Spring Boot 集成 jasypt 3.0.3 配置文件

作者: _大叔_ | 来源:发表于2020-11-23 16:04 被阅读0次

添加依赖

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

yml中添加配置文件

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

输出加密密码工具类

package com.aa.cloud.util;

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

/**
 * @author big uncle
 * @date 2020/11/23 14:23
 * @module
 **/
public class JasyptUtil {


    /**
     * Jasypt生成加密结果
     * @param password 配置文件中设定的加密盐值
     * @param value 加密值
     * @return
     */
    public static String encyptPwd(String password,String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        String result = encryptor.encrypt(value);
        return result;
    }

    /**
     * 解密
     * @param password 配置文件中设定的加密盐值
     * @param value 解密密文
     * @return
     */
    public static String decyptPwd(String password,String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setConfig(cryptor(password));
        String result = encryptor.decrypt(value);
        return result;
    }

    public static SimpleStringPBEConfig cryptor(String password){
        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");
        return config;
    }


    public static void main(String[] args) {
        // 加密
        String encPwd = encyptPwd("giant", "mysql");
        // 解密
        String decPwd = decyptPwd("giant", encPwd);
        System.out.println(encPwd);
        System.out.println(decPwd);
    }

}

得到加密密码进行替换

  redis:
    host: 192.168.81.101
    password: ENC(u1itOZa4Xt3qMyG1VJGa9fc0wDUaQ59/)
    database: 0
    port: 26379
    timeout: 10000
    sentinel:
      nodes:
        - 192.168.81.101:26379
        - 192.168.81.102:26379
      password: ENC(1iyE4/wqjqSHmFKKVVpLAg==)
      master: mymaster
      enable: true
    lettuce:
      pool:
        max-wait: 10000
        max-active: 30
        max-idle: 15
        min-idle: 15

建议部署的时候 盐 不要放到配置文件,可以用启动参数 -Djasypt.encryptor.password=aabbcc 来替代。

相关文章

网友评论

      本文标题:Spring Boot 集成 jasypt 3.0.3 配置文件

      本文链接:https://www.haomeiwen.com/subject/gelviktx.html