SpringMVC使用隱式jdbc串連資訊

來源:互聯網
上載者:User

SpringMVC使用隱式jdbc串連資訊

本地測試環境中,我們如果使用SpringMVC構建項目時,資料庫的“使用者名稱、密碼、串連地址”都直接明文寫在設定檔中。而一旦項目構建在運行環境的伺服器時,對“使用者名稱、密碼、串連地址”串連資訊進行必要的隱藏,這樣更加安全一些,那麼如何來做隱式jdbc串連資訊呢?

一、隱式jdbc串連資訊的可行方案

SpringMVC在構建jdbc串連資訊時,一般是在“applicationContext.xml”檔案中有如下資訊提供給項目的JdbcConnection。

<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%20%E5%BC%95%E5%85%A5jdbc%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%20%2D%2D%3E--><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="locations">        <value>file:C:/properties/hcmanage.properties</value>    </property></bean><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">    <property name="driverClassName" value="${driver}"></property>    <property name="url" value="${url}?useUnicode=true&characterEncoding=utf8&"></property>    <property name="username" value="${username}"></property>    <property name="password" value="${password}"></property>    <property name="testOnBorrow" value="true">    <property name="validationQuery">        <value>select 1 from DUAL</value>    </property></property></bean></code>

然後我們在hcmanage.properties檔案中配置“使用者名稱、密碼、串連地址”的明文資訊。

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/jdbcusername=rootpassword=root

這種原始的做法很直接的就暴露了我們的“使用者名稱、密碼、串連地址”等關鍵資訊,那麼如何來規避這些關鍵資訊呢?最好的做法是在hcmanage.properties檔案中我們只提供如下資訊顯示:

driver=com.mysql.jdbc.Driverurl=username=password=

我們把“使用者名稱、密碼、串連地址”真實資訊儲存在相對安全的位置,比如說我們自己的資料庫,該資料庫不在生產環境上,這樣做的話,別人要想知道生產環境上的“使用者名稱、密碼、串連地址”,就必須先破解我們自己的伺服器,然後破解該伺服器上的資料庫,相對來說增加了不少難度。

那麼想要實現這種安全的效果,我們該怎麼做呢?

關鍵位置就在“PropertyPlaceholderConfigurer”類上!

<code class="language-xml hljs "><!--{cke_protected}{C}%3C!%2D%2D%20%E5%BC%95%E5%85%A5jdbc%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%20%2D%2D%3E--><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="locations">        <value>file:C:/properties/hcmanage.properties</value>    </property></bean></code>
二、繼承PropertyPlaceholderConfigurer類

沒錯,我們建立一個自訂的PropertyPlaceholderConfigurer類,繼承”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”。

EncryptPropertyPlaceholderConfigurer.java

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {    @Override    protected String convertProperty(String propertyName, String propertyValue) {        if (isEncryptProperty(propertyName)) {            return Constants.databaseMap.get(propertyName);        }        return super.convertProperty(propertyName, propertyValue);    }    private boolean isEncryptProperty(String pname) {        for (String name : Constants.DATABASE_PROPERTY_NAMES) {            if (name.equals(pname)) {                return true;            }        }        return false;    }}

ps:注意關鍵方法

protected String convertProperty(String propertyName, String propertyValue)

該方法會根據設定檔中提供的propertyName,按照我們自己的意願進行轉換,返回對應的propertyValue。

也就是說,我們可以將

driver=com.mysql.jdbc.Driverurl=username=password=

通過一定的轉換法則,轉換為

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://127.0.0.1:3306/jdbcusername=rootpassword=root

而這個過程是不透明的,也就是所謂的隱式轉換!

三、使用webservice進行jdbc串連資訊的擷取Java實現SSH模式加密, webservice之間通訊

如何建立webservice通訊串連,你可以參照第二篇。
如何在通訊過程中進行非對稱式加密,你可以參照第一篇。

由於之前寫過類似部落格,我這裡就不再贅述,重要的是提供SpringMVC使用隱式jdbc串連資訊的解決方案!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.