数据库连接加密
2017-02-27 13:47
162 查看
在实际项目中,经常需要对数据库连接池的相关配置加密,比如说数据库的密码,下面介绍两种加密和解密方法
一,重写数据库连接池
ViewCode
然后在配置连接池时,进行如下配置
一,重写数据库连接池
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
相关文章推荐
- Enterprise Library2.0中加密数据库连接字符串
- PB数据库连接描述文件的密码加密和解密原理
- 运行时动态设置并加密App.Config中的数据库连接字符串
- 用微软企业库加密数据库连接字符串
- 动态设置并加密App.Config中数据库连接字符串
- 数据库连接字符串的处理方法!加密解密连接字符串。
- 应程程序给连接数据库字符串加密...
- ASP.NET数据库连接字符串的加密与解密
- 对web.config的数据库连接字符串进行加密
- PB数据库连接描述文件的密码加密和解密原理
- ASP.NET数据库连接字符串的加密与解密
- 加密数据库连接字符串(asp.net 2.0)
- 在Spring框架下对数据库连接的配置信息加密
- 加密数据库连接字符串(asp.net 2.0)
- Enterprise Library2.0中加密数据库连接字符串
- ASP.NET2.0关于数据库连接设置的加密解密
- ASP.NET数据库连接字符串的加密与解密
- ASP.NET数据库连接字符串的加密与解密
- IBatis.NET WebSite 项目的数据库连接加密
- 加密在 Web.Config 中存储的数据库连接字串