spring讀取加密配置資訊,spring讀取加密

來源:互聯網
上載者:User

spring讀取加密配置資訊,spring讀取加密

描述&背景
Spring架構設定資料庫等串連等屬性時,都是交由 PopertyPlaceholderConfigurer進行讀取.properties檔案的,但如果項目不允許在設定檔中明文儲存密碼等重要的串連資訊,此時,唯有繼承PopertyPlaceholderConfigurer,並重寫convertProperty(String propertyName, String propertyValue)方法,該方法是java中少有的傳參承載設計模式,在這裡,我們可以拿到我們需要進行解密的密文再傳給spring組件去串連資料庫,避免了明文儲存。所以我們可以將加密後的資訊儲存到.Properties檔案中,在讀取前解密,就可以實現不明文儲存資訊需求。這裡我將用AES來加密重要訊息。
步驟
1.導加密工具檔案
將AES加密檔案放到項目工具類的位置,如有其它加密和解密工具,可不用我這個AES加密。
2.繼承PropertyPlaceholderConfigurer

package com.openeap.common.web;import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;import com.openeap.common.utils.aes.AESEncryptor;public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{    private String[] encryptPropNames = {"jdbc.username", "jdbc.password"};      private String code = "gzxcxxxtgcyxgs01";    @Override      protected String convertProperty(String propertyName, String propertyValue)      {          //如果在加密屬性名稱單中發現該屬性          if (isEncryptProp(propertyName))          {              String decryptValue="";            try {                decryptValue = AESEncryptor.decrypt(code, propertyValue);            } catch (Exception e) {                // TODO Auto-generated catch block                e.printStackTrace();            }
         System.out.println(decryptValue); return decryptValue==""?propertyValue:decryptValue; }else { return propertyValue; } } private boolean isEncryptProp(String propertyName) { for (String encryptName : encryptPropNames) { if (encryptName.equals(propertyName)) { return true; } } return false; } }

 

AES加密,還需要一個code,這裡需要一個是16位或者16位配數的密鑰當spring讀取到帶有encryptPropNames包含的欄位時,會執行convertProperty方法進行解密
註:.propertites檔案中儲存的格式為
jdbc.username=admin
jdbc.password=123456

3.Spring設定檔配置
在spring載入屬性設定檔時,用

<bean class="com.openeap.common.web.EncryptPropertyPlaceholderConfigurer"  >         <property name="ignoreUnresolvablePlaceholders" value="true"></property>        <property name="locations">               <list>                   <value>classpath*:/application.properties</value>               </list>           </property>      </bean>

替換原來的

<context:property-placeholder ignore-unresolvable="true" location="classpath*:/application.properties" />

 

4. 計算加密後的資訊
AES加密方法中encrypt(String seek, String cleartext),例如原始值是aaa,密鑰是1234567887654321,得到的密碼是N!Kk8dwLm0z7hlGkq2dbdQ==

最後將密文資訊回填到.properties檔案中。

jdbc.username=N!Kk8dwLm0z7hlGkq2dbdQ==

至此,spring不在配置資訊裡儲存重要的明文資訊了。

如有錯誤,懇請指正,不勝感激!

===============我是分割線==============

AES加密檔案

http://pan.baidu.com/s/1jH6bM3W

如失效請郵件通知我,cngdsch@163.com

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.