数据库配置文件加密
2016-07-25 17:34
483 查看
数据库加密,查了好多资料,开始都没有解决,朋友spring mvc 都可以使用,但是我这么项目还是没有做出来;最后还是我旁边的波波大神做出来了。
方法一 springmvc通过的,其他的自己测:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<bean class="com.ott.Utils.ConvertPwdPropertyConfigurer"
p:location="classpath:jdbc.properties"
p:fileEncoding="utf-8"
/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${driverClassName}"
p:url="${url}"
p:username="${username}"
p:password="${password}" />
方法二:spring项目通过的
<!-- 定义受环境影响易变的变量 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<!-- 标准配置 -->
<value>classpath*:/application.properties</value>
<!-- 集群中节点配置 -->
<value>classpath*:/application.cluster.properties</value>
<!-- 本地开发环境配置 -->
<value>classpath*:/application.local.properties</value>
<!-- 服务器生产环境配置 -->
<value>classpath*:/application.server.properties</value>
</list>
</property>
</bean>
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="net.hehang" />
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="net.hehang.service.utlile.Test"
destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="poolPreparedStatements" value="false" />
<property name="defaultAutoCommit" value="false" />
</bean>
import java.lang.reflect.Method;
public class Test extends org.apache.commons.dbcp.BasicDataSource{
public static void main(String[] args) {
/**
* 遍历父类方法
*/
for(Method method:new Test().getClass().getMethods())
{
System.out.println(method);
}
}
/**
* 重写父类 setPassword()
*/
@Override
public synchronized void setPassword(String password) {
String dePassword=DESUtil.getDecryptString(password);//将密文password解密成明文password
super.setPassword(dePassword);//解密之后调用父类的setPassword();因为父类放中调用了一个私有变量,重写的方法不能完全代替父类方法
}
/**
* 重写父类 setUrl()
*/
@Override
public synchronized void setUrl(String url)
{
String deUrl=DESUtil.getDecryptString(url);//将密文Url解密成明文Url
super.setUrl(deUrl);
}
/**
* 重写父类 setUsername()
*/
@Override
public synchronized void setUsername(String username)
{
String deUsername=DESUtil.getDecryptString(username);//将密文Username解密成明文Username
super.setUsername(deUsername);
}
}
方法一 springmvc通过的,其他的自己测:
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
<bean class="com.ott.Utils.ConvertPwdPropertyConfigurer"
p:location="classpath:jdbc.properties"
p:fileEncoding="utf-8"
/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close"
p:driverClassName="${driverClassName}"
p:url="${url}"
p:username="${username}"
p:password="${password}" />
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; public class ConvertPwdPropertyConfigurer extends PropertyPlaceholderConfigurer{ private String[] encryptPropNames = {"driverClassName","url","username", "password"}; @Override protected String convertProperty(String propertyName, String propertyValue) { //如果在加密属性名单中发现该属性 if (isEncryptProp(propertyName)) { String decryptValue = DESUtil.getDecryptString(propertyValue); System.out.println(decryptValue); return decryptValue; }else { return propertyValue; } } private boolean isEncryptProp(String propertyName) { for (String encryptName : encryptPropNames) { if (encryptName.equals(propertyName)) { return true; } } return false; } }
import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class DESUtil { private static Key key; private static String KEY_STR="mykey"; static{ try { KeyGenerator generator = KeyGenerator.getInstance("DES"); SecureRandom secureRandom=SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(KEY_STR.getBytes()); generator.init(secureRandom); key = generator.generateKey(); generator=null; } catch (Exception e) { throw new RuntimeException(e); } } /** * 对字符串进行加密,返回BASE64的加密字符串 * <功能详细描述> * @param str * @return * @see [类、类#方法、类#成员] */ public static String getEncryptString(String str){ BASE64Encoder base64Encoder = new BASE64Encoder(); System.out.println(key); try { byte[] strBytes = str.getBytes("UTF-8"); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptStrBytes = cipher.doFinal(strBytes); return base64Encoder.encode(encryptStrBytes); } catch (Exception e) { throw new RuntimeException(e); } } /** * 对BASE64加密字符串进行解密 * <功能详细描述> * @param str * @return * @see [类、类#方法、类#成员] */ public static String getDecryptString(String str){ BASE64Decoder base64Decoder = new BASE64Decoder(); try { byte[] strBytes = base64Decoder.decodeBuffer(str); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] encryptStrBytes = cipher.doFinal(strBytes); return new String(encryptStrBytes,"UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } }
方法二:spring项目通过的
<!-- 定义受环境影响易变的变量 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<!-- 标准配置 -->
<value>classpath*:/application.properties</value>
<!-- 集群中节点配置 -->
<value>classpath*:/application.cluster.properties</value>
<!-- 本地开发环境配置 -->
<value>classpath*:/application.local.properties</value>
<!-- 服务器生产环境配置 -->
<value>classpath*:/application.server.properties</value>
</list>
</property>
</bean>
<!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
<context:component-scan base-package="net.hehang" />
<!-- 数据源配置,使用应用内的DBCP数据库连接池 -->
<bean id="dataSource" class="net.hehang.service.utlile.Test"
destroy-method="close">
<!-- Connection Info -->
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- Connection Pooling Info -->
<property name="initialSize" value="5" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="30" />
<property name="maxWait" value="500" />
<property name="poolPreparedStatements" value="false" />
<property name="defaultAutoCommit" value="false" />
</bean>
import java.lang.reflect.Method;
public class Test extends org.apache.commons.dbcp.BasicDataSource{
public static void main(String[] args) {
/**
* 遍历父类方法
*/
for(Method method:new Test().getClass().getMethods())
{
System.out.println(method);
}
}
/**
* 重写父类 setPassword()
*/
@Override
public synchronized void setPassword(String password) {
String dePassword=DESUtil.getDecryptString(password);//将密文password解密成明文password
super.setPassword(dePassword);//解密之后调用父类的setPassword();因为父类放中调用了一个私有变量,重写的方法不能完全代替父类方法
}
/**
* 重写父类 setUrl()
*/
@Override
public synchronized void setUrl(String url)
{
String deUrl=DESUtil.getDecryptString(url);//将密文Url解密成明文Url
super.setUrl(deUrl);
}
/**
* 重写父类 setUsername()
*/
@Override
public synchronized void setUsername(String username)
{
String deUsername=DESUtil.getDecryptString(username);//将密文Username解密成明文Username
super.setUsername(deUsername);
}
}
相关文章推荐
- LTrim和RTrim在SQL的用法
- .mysqldump
- oracle case when then else end
- Mysql学习中遇到的一些问题
- oracle创建表副本
- oracle不能导出空的表
- mysql 行列转换
- MongoDB学习笔记—权限管理
- mysql 主主复制搭建用的命令
- 使用Redis存储Nginx+Tomcat负载均衡集群的Session
- MAC: MySql修改root密码 mac Mysql忘记密码 mysql mac重置密码
- PL/SQL连接Oracle64位的方法
- Oracle 创建表空间
- mysql 踩过的坑
- ORACLE数据库数据导入Mysql数据库的一个小方法
- mysql访问控制实现原理
- Oracle 高水位(HWM: High Water Mark) 说明
- Oracle SQL篇(一)null值之初体验
- 为什么Redis内存不宜过大
- APICloud 创建本地数据库(SQLite)