在Spring框架下對資料庫連線的配置資訊加密

2021-04-18 22:14:42 字數 989 閱讀 7751

現在有乙個專案,客戶要求在配置檔案中的資料庫連線資訊是密文的,當時還以為很是個簡單的事。但是這個由於專案使用到了spring框架,而對資料庫的連線資訊是在spring啟動時就裝入記憶體的,因此要在載入後對檔案進行解密,幾乎不太可能。後面分析了spring 的原始碼,發現spring屬性檔案的載入都是通過propertyplaceholderconfigurer 裡面的loadproperties(properties props)方法實現的,因此想到了過載此類,並且複寫loadproperties(properties props)方法來實現對加密檔案的解密,並在這個方法中將明文傳遞給spring框架。

1.編寫類乙個屬性檔案載入類,此類過載propertyplaceholderconfigurer

public class decryptpropertyplaceholderconfigurer extends

propertyplaceholderconfigurer

public void setlocations(resource locations)

public void setfileencoding(string fileencoding)

public void loadproperties(properties props) throws ioexception

inputstream is = null;

try

if (location.getfilename().startswith("acl"))

if (this.fileencoding != null) else

} finally }}

}}注:紅色部分是加解密工具類,考慮到加解密的方法很多,讀者可以自己編寫適合自己應用的 。

2.在spring配置檔案中引如上面的類

classpath:db.properties

classpath:acl.properties

classpath:key.properties

Spring 對資料庫操作

1.spring的資料庫訪問方式 提供了對jdbc,mybatis,hibernate等的支援。主要通過dao提供了如下的支援 a.提供了與平台無關的資料庫訪問異常體系 b.提供了資料訪問的模板,比如 jbdctemplate,jdbctemplate,hibernatetemplate c.提供了...

Django框架下連線Mysql資料庫

1 找到settings.py檔案 2 新增如下 databases 3 在上面的基礎上,下面介紹兩種連線資料庫的具體方法 1 使用models.py連線 第一步 根據已存在的資料庫生成models.py檔案。進入cmd命令視窗,然後cd到你的專案中manage.py檔案所在目錄下,如 然後輸入py...

DelphiWebMVC框架下不同資料庫的使用

在系統開發是,有時我們需要鏈結不同的資料庫,連線mysql同時需要連線mssql,在框架中按照下面方法設定。開啟 udbconfig.pas 檔案 default tdbsqlite 必須有default成員變數名 mysql tdbmysql constructor create destruct...