1. 程式人生 > >springBoot資料庫連結加密

springBoot資料庫連結加密

1.引入jasypt依賴包

<!-- 資料庫加密 -->
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>1.16</version>
</dependency>

2.application.properties進行配置加密祕鑰,這個可以隨便寫(這步不能少):

jasypt.encryptor.password=EbfYkitulv73I2p0mXI50JMXoaxZTKJ7

3. 加密欄位生成,使用者名稱,密碼都可以通過同樣的方法生成加密後的字串,但是每次執行就會生成新的加密字串,但原有的加密字串還可以繼續使用,加密後的字串在進行請求連結的時候會自動進行解密連結:

import org.jasypt.encryption.StringEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.text.DecimalFormat;

@Controller
@RequestMapping("/demo")
public class Demo {


    @Autowired
    StringEncryptor encryptor;

    @GetMapping("/")
    @ResponseBody
    public void test() {
        String username = encryptor.encrypt("root");
        System.out.println(username);
        String password = encryptor.encrypt("123456");
        System.out.println(password);
    }

}

4.通過執行上邊的程式碼就會生成加密字串:

root------------------------------ONb8Jcr6LjLhUq1F8BzMuw==

123456-------------------------A/XAJC73+y+8ayiN4owwWrNdlLqM599j

再將原有的使用者名稱和密碼用加密後的字串替代就好了,記得加ENC()解密方式

jdbc.username=ENC(ONb8Jcr6LjLhUq1F8BzMuw==)
jdbc.password=ENC(A/XAJC73+y+8ayiN4owwWrNdlLqM599j)

5.啟動連線大功告成!!!!!!!!!!!!!!!!!!!!!