<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          面試官:如何保護(hù) SpringBoot 配置文件中的敏感信息

          共 4220字,需瀏覽 9分鐘

           ·

          2021-09-17 03:03

          說明

          使用過 SpringBoot 配置文件的朋友都知道,資源文件中的內(nèi)容通常情況下是明文顯示,安全性就比較低一些。

          打開application.propertiesapplication.yml,比如 MySql 登陸密碼,Redis 登陸密碼以及第三方的密鑰等等一覽無余,這里介紹一個加解密組件,提高一些屬性配置的安全性。

          jasypt 由一個國外大神寫了一個 Springboot 下的工具包,用來加密配置文件中的信息。

          GitHub Demo地址

          ?

          https://github.com/jeikerxiao/spring-boot2/tree/master/spring-boot-encrypt

          ?

          數(shù)據(jù)用戶名和數(shù)據(jù)庫密碼加密為例

          引入包

          查看最新版本可以到:

          ?

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

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

          2 配置加/解的密碼

          # jasypt加密的密匙 jasypt:   encryptor: password: Y6M9fAJQdU7jNp5MW

          測試用例中生成加密后的秘鑰

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

              @Autowired
              private StringEncryptor encryptor;

              @Test
              public void getPass() {
                  String url = encryptor.encrypt("jdbc:mysql://localhost:3306/mydb?autoReconnect=true&serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8");
                  String name = encryptor.encrypt("root");
                  String password = encryptor.encrypt("123456");
                  System.out.println("database url: " + url);
                  System.out.println("database name: " + name);
                  System.out.println("database password: " + password);
                  Assert.assertTrue(url.length() > 0);
                  Assert.assertTrue(name.length() > 0);
                  Assert.assertTrue(password.length() > 0);
              }
          }

          下面是輸出加密字符串:

          database url: 6Ut7iADnHS18cManoFJuNRQ5QEDfcho/F96SOhsHZdXlHYCa5PSrz6rk48I9eHB7qPp5AxDFBk9xi0I1hi6BJ0DSPYA9443gBAk5JDUxDufjUKsdh6knZJLNELmFJzYrDvCu4S0x22MYdZqJDLbyDUU2JcoezCvs156vmsPgU4A= 
          database name: fmai72yGYKGlP6vTtX77EQ==
          database password: GPMG7FGV+EA9iGkC27u67A==

          將加密后的字符串替換原明文

          applicatioin.yml

          server:
            port: 8080
          spring:
            # 數(shù)據(jù)庫相關(guān)配置
            datasource:
              driver-class-name: com.mysql.cj.jdbc.Driver
              # 這里加上后綴用來防止mysql亂碼,serverTimezone=GMT%2b8設(shè)置時區(qū)
              url: ENC(h20YiPrvNnuuTGjlrE1RVpudMuIQAS6ZPSVo1SPiYVyLen7/TWI5rXVRkStA3MDcoVHQCmLa70wYU6Qo8wwtnsmaXa5jykD3MNhAp5SGJxHsTG5u7tflPdnNmOufyhdsYPxBGWAgibYs9R7yBfrvtwBTRbe096APd3bnG3++Yro=)
              username: ENC(sT6BztXbJEa71eg3pPGYMQ==)
              password: ENC(MpSZFJ9ftq+3+VUANZjr0Q==)
            jpa:
              hibernate:
                ddl-auto: update
              show-sql: true
            # 返回的api接口的配置,全局有效
            jackson:
             # 如果某一個字段為null,就不再返回這個字段
              default-property-inclusion: non_null
              date-format: yyyy-MM-dd HH:mm:ss
              serialization:
                write-dates-as-timestamps: false
              time-zone: GMT+8
          # jasypt加密的密匙
          jasypt:
            encryptor:
              password: Y6M9fAJQdU7jNp5MW
          ?

          注意: 上面的 ENC() 是固定寫法.

          ?

          附言

          部署時配置 salt(鹽)值

          為了防止salt(鹽)泄露,反解出密碼.可以在項目部署的時候使用命令傳入salt(鹽)值:

          java -jar xxx.jar  -Djasypt.encryptor.password=Y6M9fAJQdU7jNp5MW

          或者在服務(wù)器的環(huán)境變量里配置,進(jìn)一步提高安全性。

          打開/etc/profile文件

          vim /etc/profile

          在 profile 文件末尾插入 salt(鹽)變量

          export JASYPT_PASSWORD = Y6M9fAJQdU7jNp5MW

          編譯,使配置文件生效

          source /etc/profile

          運行

          java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar

          文章有幫助的話,在看,轉(zhuǎn)發(fā)吧。

          謝謝支持喲 (*^__^*)

          瀏覽 31
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  美女扒开嫩嫩的尿囗让人桶出白浆 | 国产情趣视频 | 99草在线视频 | 五月婷婷丁香在线导航 | A片视频免费观看 |