您的位置:首页 > 数据库

数据库连接加密

2017-02-27 13:47 162 查看
在实际项目中,经常需要对数据库连接池的相关配置加密,比如说数据库的密码,下面介绍两种加密和解密方法

一,重写数据库连接池

importorg.apache.commons.codec.binary.Base64;
importorg.apache.commons.dbcp.BasicDataSource;

importjava.sql.SQLFeatureNotSupportedException;
importjava.util.logging.Logger;

/**
*@author:lyy
*@Date:2016/2/612:16
*@version:
*@Description:
*/
publicclassUnisConnectionextendsBasicDataSource{
privateStringsalt="unis";
privateStringmix="Th";

publicUnisConnection(){
super();
}

@Override
publicvoidsetPassword(Stringpassword){
try{
this.password=decodeUnisPassword(password);
}catch(Exceptione){

}
}

/**
*ReturntheparentLoggerofalltheLoggersusedbythisdatasource.This
*shouldbetheLoggerfarthestfromtherootLoggerthatis
*stillanancestorofalloftheLoggersusedbythisdatasource.Configuring
*thisLoggerwillaffectallofthelogmessagesgeneratedbythedatasource.
*Intheworstcase,thismaybetherootLogger.
*
*@returntheparentLoggerforthisdatasource
*@throwsSQLFeatureNotSupportedExceptionifthedatasourcedoesnotuse
*{@codejava.util.logging}
*@since1.7
*/
@Override
publicLoggergetParentLogger()throwsSQLFeatureNotSupportedException{
returnnull;
}

/**
*@author:lyy
*@Time:2016/2/613:58
*@Descrption:根据密码串,反编码出真正的数据库密码
*@paramencodeStr密码串
*@return原始密码串
*@throws
*/
privateStringdecodeUnisPassword(StringencodeStr){
Stringtemp=newString(Base64.decodeBase64(encodeStr));
temp=temp.substring(mix.length());
temp=newString(Base64.decodeBase64(temp));
returntemp.substring(salt.length());
}
}


ViewCode
然后在配置连接池时,进行如下配置


<beanid="dataSource"class="com.unisits.zngkpt.data.userprivrmandata.bojo.UnisConnection"destroy-method="close">
<propertyname="driverClassName"value="${driverClasss}"/>
<propertyname="url"value="${jdbcUrl}"/>
<propertyname="username"value="${loginusername}"/>
<propertyname="password"value="${password}"/>
<!--初始化连接大小-->
<propertyname="initialSize"value="${initialSize}"></property>
<!--连接池最大数量-->
<propertyname="maxActive"value="${maxActive}"></property>
<!--连接池最大空闲-->
<propertyname="maxIdle"value="${maxIdle}"></property>
<!--连接池最小空闲-->
<propertyname="minIdle"value="${minIdle}"></property>
<!--获取连接最大等待时间-->
<propertyname="maxWait"value="${maxWait}"></property>
</bean>


具体的属性为:


loginusername=sa
password=VGhkVzVwYzNSb2RXNXBjdz09


二,重写PropertyPlaceholderConfiger类

详见精通spring4.0--<6.3.2



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐
章节导航