SpringBoot下配置文件密码加密


一、导入配置文件


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

二、执行测试:生成密文密码

1.加盐:在application.properties中添加(自行设置),加/解密规则

#加/解密的时候用这个密码
jasypt.encryptor.password=zhangzhixi

2.执行测试文件:用于生成密码

import org.jasypt.encryption.StringEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @ClassName com.zhixi.MyTest
 * @Author zhangzhixi
 * @Description
 * @Date 2022-4-22 10:57
 * @Version 1.0
 */
@SpringBootTest
public class MyTest {

    @Autowired
    StringEncryptor stringEncryptor;

    @Test
    public void encryptorTest() {
        /*你的数据库密码*/
        String passwd = stringEncryptor.encrypt("you-passwd");
        System.out.println("加密密码是:" + passwd);

        System.out.println("解密密码是:"+stringEncryptor.decrypt(passwd));
    }
}

3.将生成的密码添加到数据库连接信息中:注意要使用ENC()包裹住生成的密码  

spring.datasource.password=ENC(LVwCic/f7N63bxiCWAoCq0IMOVjjGr69zalGkmbOaPVSz+k74hyVvdPLdBuo5)

4.删除加盐的规则

这个主要是防止别人看到你的加盐规则,可以得到你的密码。

三、启动项目

如果注释了加盐的规则,就需要在项目启动的时候做一些操作:

IDEA启动项目:

命令行下启动项目,加上参数:--jasypt.encryptor.password=xxx