且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PropertyPlaceholderConfigurer扩展,隐藏jdbc.properties中的用户名、密码、连接地址

更新时间:2022-08-14 21:04:10

SpringMVC项目一旦部署到服务器上时,关于数据库配置文件jdbc.properties中的“用户名、密码、连接地址”等关键信息,进行必要的加密工作显得尤为重要,那么本篇来介绍如何扩展org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类,通过webservice的方式进行“用户名、密码、连接地址”的获取,而不再使用明文的jdbc.properties文件。


一、隐式jdbc连接信息的可行方案


SpringMVC在构建jdbc连接信息时,一般是在“applicationContext.xml”文件中有如下信息提供给项目的JdbcConnection。

<!-- 引入jdbc配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>file:C:/properties/jdbc.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&amp;characterEncoding=utf8&amp;"></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>
</bean>


然后我们在jdbc.properties文件中配置“用户名、密码、连接地址”的明文信息。


driver=com.mysql.jdbc.Driver

url=jdbc:mysql://127.0.0.1:3306/jdbc

username=root

password=root


这种原始的做法很直接的就暴露了我们的“用户名、密码、连接地址”等关键信息,一旦服务器被攻破,数据库信息瞬间就暴露无遗,那么如何来规避这些关键信息呢?***的做法(不显示关键信息)是在jdbc.properties文件中我们只提供如下信息显示:


driver=com.mysql.jdbc.Driver

url=

username=

password=


我们把“用户名、密码、连接地址”真实信息保存在相对安全的位置,比如说我们自己的数据库,该数据库不在生产环境上,这样做的话,别人要想知道生产环境上的“用户名、密码、连接地址”,就必须先破解我们自己的服务器,然后破解该服务器上的数据库,相对来说增加了不少难度。


那么想要实现这种安全的效果,我们该怎么做呢?


关键位置就在“PropertyPlaceholderConfigurer”类上!


<!-- 引入jdbc配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <value>file:C:/properties/jdbc.properties</value>
    </property>
</bean>


二、扩展PropertyPlaceholderConfigurer类


没错,我们新建一个自定义的PropertyPlaceholderConfigurer类,继承”org.springframework.beans.factory.config.PropertyPlaceholderConfigurer”,对关键的“用户名、密码、连接地址”进行转换。


EncryptPropertyPlaceholderConfigurer.java
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
    String[] DATABASE_PROPERTY_NAMES = { "username", "password", "url" };
    private static Map<String, String> databaseMap = null;//TODO 此处换成你的加密方式获取,格式为{username:xxxxx}
    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        if (isEncryptProperty(propertyName)) {
            return databaseMap.get(propertyName);
        }
        return super.convertProperty(propertyName, propertyValue);
    }
    private boolean isEncryptProperty(String pname) {
        for (String name : 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.Driver

url=

username=

password=


通过一定的转换法则,转换为


driver=com.mysql.jdbc.Driver

url=jdbc:mysql://127.0.0.1:3306/jdbc

username=root

password=root



而这个过程是不透明的,也就是所谓的隐式转换!


三、使用webservice进行jdbc连接信息的获取


Java实现SSH模式加密,

webservice之间通信

如何建立webservice通信连接,你可以参照第二篇。

如何在通信过程中进行非对称加密,你可以参照第一篇。


由于之前写过类似博客,我这里就不再赘述,重要的是提供SpringMVC使用隐式jdbc连接信息的解决方案!