springboot yml数据连接文件用户名、密码直接显示,对数据库的安全不利。springboot有一套支持用户、密码加密的支持:jasypt 全称:jasypt-spring-boot-starter
话不多说直接来干的:
1、pom文件添加依赖
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
2、生成密码工具
import org.jasypt.util.text.BasicTextEncryptor;
/**
* 加密算法
*/
public class EncryptUtils {
public static void main(String[] arg){
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
//加密所需的salt
textEncryptor.setPassword("123");
//要加密的数据(数据库的用户名或密码)
String username = textEncryptor.encrypt("username");
String password = textEncryptor.encrypt("password");
System.out.println("username:"+username);
System.out.println("password:"+password);
}
}
3、yml 文件添加
jasypt:
encryptor:
password: 123
用户名:ENC()
密码:ENC()
4、项目配制(不配制也可以)
VM options:-Djasypt.encryptor.password=123 (添加password)