将配置文件中里的数据库信息进行加密
实现方案:
1.导包
<!--数据库信息加密-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
2.配置盐 (盐自行设置)
3.根据明文生成密文
不能直接用main方法或@test跑
建一个util类
package com.msunsoft.web.util;
import org.jasypt.encryption.StringEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
/**
* @author yuanhao
* @create 2022-04-08 14:37
*/
@SpringBootTest
public class jasyptUtil {
@Resource
private StringEncryptor encryptor;
@Test
public void getPass(){
String url_clear_text ="jdbc:oracle:thin:@127.0.0.1:1521/orcl";
String url_cipher_text= encryptor.encrypt(url_clear_text);
String name_clear_text= "root";
String name_cipher_text=encryptor.encrypt(name_clear_text);
String password_clear_text ="123456";
String password_cipher_text=encryptor.encrypt(password_clear_text);
String mname="ZMgpYPzo75NYPYWHMFoqiw==";
String name2=encryptor.decrypt(mname);
System.out.println("------------------输出结果-------------------");
System.out.println("database url:" +url_cipher_text);
System.out.println("database name:" +name_cipher_text);
System.out.println("database password: "+ password_cipher_text);
System.out.println(name2);
}
}
输出结果将密文进行替换
注意:替换密文时要加 ENC( )
@SpringBootTest依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>