Spring-Boot数据库密码加密配置

springboot集成mysql/oracle时需要在yml/properties中配置数据库信息,用户名密码是肯定有的,所以就涉及到密码的加密,当然不加密也是可以的,正如某位大佬所说的,不加密就像是在裸奔。。。

这么说来,我已经裸奔很久了,艾玛,我也是厉害

today let us 穿上 衣服 奔跑

1.在pom文件中添加 jar 关于加密解密的

     <!-- https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter -->
        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>1.8</version>
        </dependency>

2.编写测试类

import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @ClassName EncodeTest
 * @Description 加密工具类 梦想家
 * @Author Zhai XiaoTao https://www.cnblogs.com/zhaiyt
 * @Date 2018/12/28 13:24
 * @Version 1.0
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class EncodeTest {

    @Autowired
    StringEncryptor stringEncryptor;

    @Test
    public void encryptPwd() {
        //加密密码
        String pwd = stringEncryptor.encrypt("your password");
        System.out.println(pwd);
    }
}

3.修改yml 或 properties 文件

在配置文件中加入:

jasypt:
  encryptor:
    password: 你的密钥

运行加密工具类 得到密码  将密码修改

password: ENC(加密方法得到的密码)

OK,完美,妈妈再也不担心你裸奔了