<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>

          Spring Boot 敏感信息加密

          共 2967字,需瀏覽 6分鐘

           ·

          2022-02-18 04:30

          ??Java大聯(lián)盟

          ? 致力于最高效的Java學(xué)習(xí)

          關(guān)注



          B 站搜索:楠哥教你學(xué)Java

          獲取更多優(yōu)質(zhì)視頻教程


          使用過(guò)SpringBoot配置文件的朋友都知道,資源文件中的內(nèi)容通常情況下是明文顯示,安全性就比較低一些。
          打開(kāi)application.propertiesapplication.yml,比如 MySql登陸密碼,Redis登陸密碼以及第三方的密鑰等等一覽無(wú)余,這里介紹一個(gè)加解密組件,提高一些屬性配置的安全性。
          jasypt由一個(gè)國(guó)外大神寫(xiě)了一個(gè)springboot下的工具包,用來(lái)加密配置文件中的信息。
          GitHub Demo地址:
          https://github.com/jeikerxiao/spring-boot2/tree/master/spring-boot-encrypt

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

          1. 引入包

          查看最新版本可以到:
          https://github.com/ulisesbocchio/jasypt-spring-boot
          <dependency>        <groupId>com.github.ulisesbocchiogroupId>        <artifactId>jasypt-spring-boot-starterartifactId>        <version>2.1.0version>dependency>

          2. 配置加/解的密碼

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


          3. 測(cè)試用例中生成加密后的秘鑰

          @RunWith(SpringRunner.class)@SpringBootTestpublic 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==

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

          applicatioin.ymlserver:  port: 8080spring:    datasource:    driver-class-name: com.mysql.cj.jdbc.Driver        url: ENC(h20YiPrvNnuuTGjlrE1RVpudMuIQAS6ZPSVo1SPiYVyLen7/TWI5rXVRkStA3MDcoVHQCmLa70wYU6Qo8wwtnsmaXa5jykD3MNhAp5SGJxHsTG5u7tflPdnNmOufyhdsYPxBGWAgibYs9R7yBfrvtwBTRbe096APd3bnG3++Yro=)    username: ENC(sT6BztXbJEa71eg3pPGYMQ==)    password: ENC(MpSZFJ9ftq+3+VUANZjr0Q==)  jpa:    hibernate:      ddl-auto: update    show-sql: true    jackson:       default-property-inclusion: non_null    date-format: yyyy-MM-dd HH:mm:ss    serialization:      write-dates-as-timestamps: false    time-zone: GMT+8
          jasypt: encryptor: password: Y6M9fAJQdU7jNp5MW
          注意: 上面的 ENC() 是固定寫(xiě)法.
          附言

          部署時(shí)配置salt(鹽)值

          為了防止salt(鹽)泄露,反解出密碼.可以在項(xiàng)目部署的時(shí)候使用命令傳入salt(鹽)值:
          java -jar xxx.jar -Djasypt.encryptor.password=Y6M9fAJQdU7jNp5MW
          或者在服務(wù)器的環(huán)境變量里配置,進(jìn)一步提高安全性.
          打開(kāi)/etc/profile文件
          vim /etc/profile
          profile文件末尾插入salt(鹽)變量
          export JASYPT_PASSWORD = Y6M9fAJQdU7jNp5MW
          編譯,使配置文件生效
          source /etc/profile
          運(yùn)行
          java -jar -Djasypt.encryptor.password=${JASYPT_PASSWORD} xxx.jar



          推薦閱讀

          1、Spring Boot+Vue項(xiàng)目實(shí)戰(zhàn)

          2、B站:4小時(shí)上手MyBatis Plus

          3、一文搞懂前后端分離

          4、快速上手Spring Boot+Vue前后端分離


          楠哥簡(jiǎn)介

          資深 Java 工程師,微信號(hào)?southwindss

          《Java零基礎(chǔ)實(shí)戰(zhàn)》一書(shū)作者

          騰訊課程官方 Java 面試官今日頭條認(rèn)證大V

          GitChat認(rèn)證作者,B站認(rèn)證UP主(楠哥教你學(xué)Java)

          致力于幫助萬(wàn)千 Java 學(xué)習(xí)者持續(xù)成長(zhǎng)。




          有收獲,就點(diǎn)個(gè)在看?
          瀏覽 52
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <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|