Spring Boot 配置文件中数据库 账户/密码加密

1、在所在的pom文件中增加依赖

 
 
         com.github.ulisesbocchio
          jasypt-spring-boot-starter
          2.0.0

2、新建JasyptUtils工具类、生成在配置文件中填写的密文

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

/**
 * 数据库密码加密  工具类   生成加密后的账户、密码*/
public class JasyptUtils {


    public static void main(String[] arg){

        StandardPBEStringEncryptor standardPBEStringEncryptor =new StandardPBEStringEncryptor();
        /*配置文件中algorithm对应的算法名:PBEWithMD5AndDES*/
        standardPBEStringEncryptor.setAlgorithm("PBEWithMD5AndDES");
        /*配置文件中password 对应的值(这个值自己随便定义)*/
        standardPBEStringEncryptor.setPassword("ZIJIDINGYI");
        /*获得name、password的加密文本*/
        String name = standardPBEStringEncryptor.encrypt("root");    //账户
        String password =standardPBEStringEncryptor.encrypt("123456");   //密码
        /*以下打印的就是在配置文件中需要配置的密文*/
        System.out.println("name:"+name);
        System.out.println("password:"+password);
    }

}

3、配置文件yml

#新增 jasypt 配置   start
jasypt:
    encryptor:
        algorithm: PBEWithMD5AndDES
        password: ZIJIDINGYI       #步骤2中 standardPBEStringEncryptor.setPassword的值一致
#新增 jasypt 配置    end



#修改数据源配置的username、password值
.......
url: 
username: ENC(步骤2 name 生成的密文)
password: ENC(步骤2 password 生成的密文)

4、测试验证

你可能感兴趣的:(Spring,Boot,Java,数据库,spring,boot,java)