数据库配置文件加密
来源:互联网 发布:win xp制作mac 启动 编辑:程序博客网 时间:2024/06/14 02:30
数据库加密,查了好多资料,开始都没有解决,朋友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}" />
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项目通过的
<!-- 定义受环境影响易变的变量 --><beanclass="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); }}
1 0
- 数据库配置文件加密
- 数据库配置文件加密
- 数据库配置文件加密
- 数据库配置文件加密
- ssh中数据库配置文件加密方法
- 配置文件加密方案——Derby嵌入式数据库的应用
- 在OpenErp的配置文件中, 为数据库密码加密
- Openerp在配置文件中为数据库密码加密
- 加密配置文件 和 解密配置文件
- 网站配置文件加密
- hibernate配置文件加密-base64
- 对配置文件进行加密
- Hibernate配置文件加密解决方案
- J2EE配置文件加密
- 如何为配置文件加密
- C#配置文件加密
- JAVA,hibernate配置文件加密
- 加密配置文件节点
- Hadoop真的适合你吗?
- iOS开发笔记-AL下label换行问题preferredMaxLayoutWidth
- VoLTE的前世今生...说清楚VoIP、VoLTE、CSFB、VoWiFi、SIP、IMS那些事
- C++学习 - 模板函数变长参数
- jquery 模糊匹配
- 数据库配置文件加密
- 夏梦悠长,不负锦年
- Android Studio Ctrl+Alt+Down 快速复制代码失效
- 14A - Letter
- java实现post方法
- 什么是回调函数?
- poj 2398 Toy Storage 计算几何+二分
- webdriver 关于元素的常用操作 python版本
- EXT2文件系统简介