1. 程式人生 > 其它 >13、SpringBoot-配置檔案裡密碼加密

13、SpringBoot-配置檔案裡密碼加密

系列導航

springBoot專案打jar包

1、springboot工程新建(單模組)

2、springboot建立多模組工程

3、springboot連線資料庫

4、SpringBoot連線資料庫引入druid

5、SpringBoot連線資料庫引入mybatis

6、SpringBoot-mybatis分頁實現pagehelper

7、SpringBoot-mybatis-plus引入

8、SpringBoot 事務

9、SpringBoot-mybatis-druid多源資料多源資料

10、SpringBoot-mybatis-plus-druid多源資料

11、SpringBoot-mybatis-plus-druid多源資料事務

12、SpringBoot-mybatis-plus-ehcache

13、SpringBoot-配置檔案裡密碼加密

未完待續

springboot連線資料庫,資料庫的使用者名稱、密碼預設多是明文放在配置檔案裡,如何提高安全性不要明文寫在配置檔案裡,不問就解決這個問題。

1、資料庫中建立表

CREATE TABLE TEST_BLOCK_T   
(
  BLOCK_ID         VARCHAR2(10 BYTE) PRIMARY   KEY,    --編碼
  BLOCK_NAME       VARCHAR2(200 BYTE)                    --資源名稱 
); Insert into TEST_BLOCK_T (BLOCK_ID, BLOCK_NAME) Values ('1', '哈哈哈'); COMMIT;

2、pom.xml依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency
> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!--密碼加密--> <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> </dependencies>

3、工程結構

4、原始碼

package com.example.demo.controller;


import org.jasypt.encryption.StringEncryptor;
import org.jasypt.util.text.BasicTextEncryptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    StringEncryptor stringEncryptor;


    //訪問資料庫的資料
    @GetMapping("/list")
    @ResponseBody
    public String index() {

        String sql = "SELECT BLOCK_NAME FROM TEST_BLOCK_T WHERE BLOCK_ID = ?";

        // 通過jdbcTemplate查詢資料庫
        String mobile = (String) jdbcTemplate.queryForObject(sql, new Object[]{1}, String.class);

        return "Hello " + mobile;
    }

    //對配置檔案中的使用者名稱密碼加密,加密鹽也是用配置檔案裡的
    @GetMapping("/passwd")
    @ResponseBody
    public String passwd() {

        //加密密碼
        String name = stringEncryptor.encrypt("zy");
        String pwd = stringEncryptor.encrypt("1");
        System.out.println("name:"+name);
        System.out.println("pwd:"+pwd);
        return "success!";

    }

    //解密配置檔案中的使用者名稱和密碼,加密鹽也是用配置檔案裡的
    @GetMapping("/unpasswd")
    @ResponseBody
    public String unpasswd() {
        //解密密碼
        String name =  stringEncryptor.decrypt("YCfAQQPOSw5Jp/uzmA8LkQ==");
        String pwd = stringEncryptor.decrypt("hOwW0zxYpHaEH/lkpHyJaA==");
        System.out.println("name:"+name);
        System.out.println("pwd:"+pwd);
        return "success!";

    }

    //對配置檔案中的使用者名稱密碼加密,加密鹽使用程式碼裡的
    @GetMapping("/passwd1")
    @ResponseBody
    public String passwd1() {

        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt(鹽),自定義
        textEncryptor.setPassword("hello");
        //要加密的資料(資料庫的使用者名稱或密碼)
        String username = textEncryptor.encrypt("zy");
        String password = textEncryptor.encrypt("1");
        System.out.println("username:"+username);
        System.out.println("password:"+password);

        return "success!";


    }




}
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

5、配置檔案

# 應用名稱
spring.application.name=demo
# 應用服務 WEB 訪問埠
server.port=8080

# 資料庫設定
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@192.168.0.100:1521:orcl
#spring.datasource.username=zy
#spring.datasource.password=1
jasypt.encryptor.password=hello
spring.datasource.username=ENC(YCfAQQPOSw5Jp/uzmA8LkQ==)
spring.datasource.password=ENC(hOwW0zxYpHaEH/lkpHyJaA==)

6、測試

(1)使用者名稱密碼明文連線資料庫

配置檔案

 

 

訪問查詢資料庫的介面 http://localhost:8080/hello/list

返回值:Hello 哈哈哈

(2)使用者名稱密碼明文連線資料庫

<1>配置檔案中開啟jasypt.encryptor.password的引數,讓系統知道加密鹽的值。

完整配置如下:

 

 

<2>呼叫介面 http://localhost:8080/hello/passwd  獲取加密後的使用者名稱和密碼

輸出的結果是:

name:YCfAQQPOSw5Jp/uzmA8LkQ==

pwd:hOwW0zxYpHaEH/lkpHyJaA==

修改配置檔案如下,其中EMC()就是告訴系統裡面的內容是加密過的需要解密

 

 

<3>重啟專案 呼叫介面 http://localhost:8080/hello/list

正常返回資料結果:Hello 哈哈哈

到此給配置檔案中的敏感資訊加密就完成了。這樣做有個缺點就是加密鹽是在配置檔案裡的,對方如果非要解密是可以做到的,例如:

<4>密文解密

將加密後的密文放到如下程式碼中

 

 

呼叫介面http://localhost:8080/hello/unpasswd

結果:

name:zy

pwd:1

到此成功解密出了之前加密的密文。所以之前的加密只能攔住不懂這個加密的人,真正懂得人是攔不住的依然可以解密出來。那還有沒有更安全一些的做法?有

<5>啟動時指定加密鹽

先將程式打成jar包 如何打包參考https://www.cnblogs.com/yclh/p/15947054.html

打包後將配置檔案中的如下這段去掉,配置檔案中就沒有加密鹽的資訊了
jasypt.encryptor.password=hello
啟動的jar包的時候使用如下語句,其中“hello”就是加密鹽,SpringBoot_PassWord.jar就是打好的jar包

java -jar -Djasypt.encryptor.password=hello SpringBoot_PassWord.jar

 

啟動成功後訪問介面:http://localhost:8080/hello/list

成功返回引數:Hello 哈哈哈

 

還有沒有其他更安全的方式,網上看到也有單獨搞個配置檔案,設定環境變數等操作起來有點麻煩,個人感覺就沒有絕對安全的,只要想破解的人懂你設定的那個套路就能破解,不管你把這個加密鹽藏在哪裡總歸是有個地方存的。所以最好的加密就是你搞得東西別人不懂,懂得人欺負不懂得人。