springboot配置文件數據庫賬號密碼加密


配置文件配置暴露一些密碼問題處理:

jasypt 是一個簡單易用的加解密Java庫

相關可以參考的國外大神寫的源碼的github地址

github:https://github.com/ulisesbocchio/jasypt-spring-boot

https://github.com/gxing19/Spring-Boot-Example/tree/master/spring-boot-password-encrypt

以下是修改步驟(我用的是springboot框架):

1、添加依賴:在pom.xml添加如下依賴

<!-- jasypt加密 -->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>

2、配置文件設置 jasypt 密鑰如:

jasypt.encryptor.password=vh^onsYFUx^DMCKK

3、編寫測試代碼生成密文
package com.aisino.cma;

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;
/**
* <p>
* 前端控制器
* </p>
*
* @author ranqw
* @since 2020-06-03
*/

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationProperties {

@Autowired
StringEncryptor encryptor;

@Test
public void jacketEncrypt() {

//加密
/*String name = encryptor.encrypt("XXX");
String password = encryptor.encrypt("XXX");
System.out.println("name 密文: " + name);
System.out.println("password 密文: " + password);*/

//解密
/*String decrypt1 = encryptor.decrypt(name);
String decrypt2 = encryptor.decrypt(password);
System.out.println(decrypt1 + "------------" + decrypt2);*/
}
}

4、spring.datasource.username 及 spring.datasource.password 配置生成的賬密
重啟應用,查看數據庫是否連接成功

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM