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,完美,媽媽再也不擔心你裸奔了

 


免責聲明!

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



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