栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot使用jasypt加密配置文件中的敏感信息

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot使用jasypt加密配置文件中的敏感信息

1、导入pom.xml


    com.github.ulisesbocchio
    jasypt-spring-boot-starter
    3.0.2
2、yml配置中引入
# 配置文件加密key
jasypt:
  encryptor:
    password: Lian
3、Jasypt加解密工具类
package com.example.springbootdemo;
 
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
 
public class jsyptPassword {
 
    private static final String PBEWITHMD5ANDDES = "PBEWithMD5AndDES";
    private static final String PBEWITHHMACSHA512ANDAES_256 = "PBEWITHHMACSHA512ANDAES_256";
 
    public static SimpleStringPBEConfig encryJsypt(String password){
        //加解密配置
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(password);
        config.setAlgorithm(PBEWITHHMACSHA512ANDAES_256);
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        return config;
    }
 
    public static String encryptPassword(String password, String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = encryJsypt(password);
        encryptor.setConfig(config);
        String resutl_encrypt = encryptor.encrypt(value);
        return resutl_encrypt;
    }
 
    public static String  decryptPassword(String password, String value){
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = encryJsypt(password);
        encryptor.setConfig(config);
        String result_decrypt = encryptor.decrypt(value);
        return result_decrypt;
    }
 
    public static void main(String[] args) {
        //第一个参数修改记得yml中也要同步修改
        String encrypt = encryptPassword("Lian", "root");
        System.out.println("root加密后:" + encrypt);
 
        String decrypt = decryptPassword("Lian", "kXRzoLJzzKjhaaZ7gq/Upy1lKKIPgE9BePJgydcXo+Qm/rc2wtEMgUPlb7yEPytv");
        System.out.println("root解密后:" + decrypt);
    }
 
 
}
4、yml中使用

ENC(密文)

spring:
 datasource:
  url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
  username: root
  password: ENC(kXRzoLJzzKjhaaZ7gq/Upy1lKKIPgE9BePJgydcXo+Qm/rc2wtEMgUPlb7yEPytv)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/979270.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号