最近很多公司因为安全问题被各种折磨,我们公司出于这方面的考虑,需要对数据库连接进行加密,在网上很容易就找到了解决方案,在这里分享给大家,但是这个解决方案的内容,也引起了我对程序的思考!
Spring管理数据库连接
<span style="font-size:18px;"><!—加密解密类--> <!—加密解密类:因为它继承了配置类,所以它可以更改配置规则--> <beanclass="com.zlwy.common.util.ZLWYPropertyPlaceholderConfigurer"> <propertyname="location"> <!—加载此配置文件之前走加密类--> <value>classpath:config.properties</value> </property> <propertyname="fileEncoding"> <value>utf-8</value> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <propertyname="driverClassName"> <!--<value>com.microsoft.sqlserver.jdbc.SQLServerDriver</value> --> <!--<value>com.mysql.jdbc.Driver</value> --> <!--<value>oracle.jdbc.driver.OracleDriver</value> --> <value>${ClassString}</value> </property> <property name="url"> <!--数据库配置文件从配置文件读取--> <value>${ConnectionString}</value> <!--<value>jdbc:mysql://localhost:3306/frame?useOldAliasMetadataBehavior=true</value>--> <!--<value>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL =TCP)(HOST=localhost)(PORT=1521))(LOAD_BALANCE=YES))(CONNECT_DATA=(SERVICE=DEDICATED)(SERVICE_NAME=orcl)))</value>--> <!-- <value>jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL= TCP)(HOST=172.20.104.230)(PORT=1521))(LOAD_BALANCE=YES))(CONNECT_DATA=(SERVICE=DEDICATED)(SERVICE_NAME=orcl)))</value> --> </property> <propertyname="username"> <value>${UserName}</value> </property> <propertyname="password"> <value>${PassWord}</value> </property> <propertyname="maxActive"> <value>1000</value> </property> <propertyname="maxIdle"> <value>500</value> </property> <propertyname="maxWait"> <value>5000</value> </property> <!-- <propertyname="validationQuery"> <value>select 1 from dual</value> </property> --> </bean></span>
<span style="font-size:18px;">packagecom.zlwy.common.util; importjava.io.IOException; importjava.util.Properties; importorg.springframework.beans.factory.config.PropertyPlaceholderConfigurer; importsun.misc.BASE64Decoder; importsun.misc.BASE64Encoder; /** * <p>Title : 中国铁路总公司社会保障管理系统[数据库配置文件中的加密配置]</p> * <p>Description: [出于安全的考虑,我们将数据库链接的用户名密码在配置文件中进行加密, * 并在此进行逻辑解密]</p> * <p>Copyright : Copyright (c) 2015</p> * <p>Company : 铁科院电子所</p> * <p>Department : 动车部</p> * @author : lujc * @version : 1.0 * @date 2015-1-16 */ publicclass ZLWYPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { @Override protected void convertProperties(Properties properties) { try { BASE64Decoder decoder = newBASE64Decoder(); String classString = new String(decoder.decodeBuffer(properties.getProperty("ClassString"))); String connectionString = newString(decoder.decodeBuffer(properties.getProperty("ConnectionString"))); String userName = newString(decoder.decodeBuffer(properties.getProperty("UserName"))); String password = newString(decoder.decodeBuffer(properties.getProperty("PassWord"))); //映射到咱们的配置文件中,这个可以通过程序更改 properties.put("ClassString",classString); properties.put("ConnectionString",connectionString); properties.put("UserName",userName); properties.put("PassWord",password); System.out.println("解析数据库加密参数完成...."); } catch (IOExceptione) { e.printStackTrace(); System.out.println("解析数据库加密参数失败...."); } } public static void main(String[] args) throwsException{ String msg = "semssems"; BASE64Encoder encode = newBASE64Encoder(); String jiami =encode.encode(msg.getBytes()); BASE64Decoder decoder = newBASE64Decoder(); String jiemi = newString(decoder.decodeBuffer(jiami)); System.out.println("加密前:"+msg); System.out.println("加密后"+jiami); System.out.println("解密后"+jiemi); } }</span>
原文地址:http://blog.csdn.net/xvshu/article/details/43853281