为了使application.properties文件中的数据库连接用户名和密码保存成密文,我们做如下处理:首先使用加密程序算出密文,然后spring 加载配置文件的时候再解密,其实就是在数据库连接池调用之前进行了解密工作。
原配置文件如:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver jdbc.url=jdbc:oracle:thin:@192.168.0.1:1521:orcl jdbc.username=oracle jdbc.password=oracle
加密后的配置文件[生成此密文的秘钥和下面常量里面的秘钥不同]:
jdbc.driverClassName=eb04a2effc62b4119af9fc911259d3346835eff4edf1de6bbfd187f7a2c73828 jdbc.url=ecb893755ede111c849a884924d16f1ea3874f16546e457950d8a541d3b2aee1ce7bd2073ebd0e3f jdbc.username=ff91f4d795574a71 jdbc.password=ff91f4d795574a71
我使用的javax.crypto.Cipher加密解密类,根据项目使用不同可以随意更换自己的加密解密算法,之后添加PropertyPlaceholderConfigurer的子类,在processProperties方法中解密即可,代码如下:
import java.util.Properties; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanInitializationException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import com.joinsoft.LoginConstants; public class EncryptablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer{ private static final String key = LoginConstants.JDBC_DESC_KEY; protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) throws BeansException { try { EncryptionDecryption decryption = new EncryptionDecryption(key); String username = props.getProperty(LoginConstants.JDBC_DATASOURCE_USERNAME_KEY); if (username != null) { props.setProperty(LoginConstants.JDBC_DATASOURCE_USERNAME_KEY, decryption.decrypt(username)); } String password = props.getProperty(LoginConstants.JDBC_DATASOURCE_PASSWORD_KEY); if (password != null) { props.setProperty(LoginConstants.JDBC_DATASOURCE_PASSWORD_KEY, decryption.decrypt(password)); } String url = props.getProperty(LoginConstants.JDBC_DATASOURCE_URL_KEY); if (url != null) { props.setProperty(LoginConstants.JDBC_DATASOURCE_URL_KEY, decryption.decrypt(url)); } String driverClassName = props.getProperty(LoginConstants.JDBC_DATASOURCE_DRIVERCLASSNAME_KEY); if(driverClassName != null){ props.setProperty(LoginConstants.JDBC_DATASOURCE_DRIVERCLASSNAME_KEY, decryption.decrypt(driverClassName)); } super.processProperties(beanFactory, props); } catch (Exception e) { e.printStackTrace(); throw new BeanInitializationException(e.getMessage()); } } }
附1,加密解密程序:
import java.security.Key; import java.security.Security; import javax.crypto.Cipher; import com.sun.crypto.provider.SunJCE; public class EncryptionDecryption { private static String strDefaultKey = "goodluck"; private Cipher encryptCipher = null; private Cipher decryptCipher = null; public static String byteArr2HexStr(byte[] arrB) throws Exception { int iLen = arrB.length; StringBuffer sb = new StringBuffer(iLen * 2); for (int i = 0; i < iLen; i++) { int intTmp = arrB[i]; while (intTmp < 0) { intTmp = intTmp + 256; } if (intTmp < 16) { sb.append("0"); } sb.append(Integer.toString(intTmp, 16)); } return sb.toString(); } public static byte[] hexStr2ByteArr(String strIn) throws Exception { byte[] arrB = strIn.getBytes(); int iLen = arrB.length; byte[] arrOut = new byte[iLen / 2]; for (int i = 0; i < iLen; i = i + 2) { String strTmp = new String(arrB, i, 2); arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16); } return arrOut; } public EncryptionDecryption() throws Exception { this(strDefaultKey); } public EncryptionDecryption(String strKey) throws Exception { Security.addProvider(new SunJCE()); Key key = getKey(strKey.getBytes()); encryptCipher = Cipher.getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE, key); decryptCipher = Cipher.getInstance("DES"); decryptCipher.init(Cipher.DECRYPT_MODE, key); } public byte[] encrypt(byte[] arrB) throws Exception { return encryptCipher.doFinal(arrB); } public String encrypt(String strIn) throws Exception { return byteArr2HexStr(encrypt(strIn.getBytes())); } public byte[] decrypt(byte[] arrB) throws Exception { return decryptCipher.doFinal(arrB); } public String decrypt(String strIn) throws Exception { try { return new String(decrypt(hexStr2ByteArr(strIn))); } catch (Exception e) { return ""; } } private Key getKey(byte[] arrBTmp) throws Exception { byte[] arrB = new byte[8]; for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) { arrB[i] = arrBTmp[i]; } Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES"); return key; } }
附2,常量定义:
//jdbc文件加密 public static final String JDBC_DESC_KEY = "missyou"; public static final String JDBC_DATASOURCE_USERNAME_KEY = "jdbc.username"; public static final String JDBC_DATASOURCE_PASSWORD_KEY = "jdbc.password"; public static final String JDBC_DATASOURCE_URL_KEY = "jdbc.url"; public static final String JDBC_DATASOURCE_DRIVERCLASSNAME_KEY = "jdbc.driverClassName";
相关推荐
NULL 博文链接:https://wokeke.iteye.com/blog/2214113
Spring中PropertyPlaceholderConfigurer的使用
Spring属性占位符PropertyPlaceholderConfigurer的使用,自己写的一个demo
主要介绍了Spring如何使用PropertyPlaceholderConfigurer读取文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
在Spring3中配置数据源,包括DBCP,C3P0,Proxool,Bonecp主要的数据源,里面包含这些数据源的jar文件和依赖文件及配置文件。。 如Bonecp目前听说是最快的数据源,速度是传统的c3p0的25倍, bonecp.properties文件: ...
6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. 其它资源 7. Spring AOP APIs 7.1. 简介 7.2. Spring中的切入点API 7.2.1. 概念 7.2.2. 切入点实施 ...
这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...
使用Spring.NET进行面向方面的编程 12.1.简介 12.1.1.AOP基本概念 12.1.2.Spring.NET AOP的功能 12.1.3.Spring.NET的AOP代理 12.2.Spring.NET中的切入点 12.2.1.概念 12.2.2.切入点的操作 12.2.3.Spring.NET提供的...
不使用XML定义档进行 Bean设置 Aware 相关介面 BeanPostProcessor BeanFactoryPostProcessor PropertyPlaceholderConfigurer PropertyOverrideConfigurer CustomEditorConfigurer ...
#Spring 属性注释扩展的 PropertyPlaceHolderConfigurer 使用注解将配置属性注入到 Spring 组件中。 注意:Spring 3 现在支持使用 @Value 注释的容器的。 该项目仅用于 Spring 2.5.x 支持。 ##入门Spring房产注解...
NULL 博文链接:https://chyy001.iteye.com/blog/1338488
前几天搞 Spring + Hibernate + Struts 事务配置 ,网上找了好多资料,不过好无语,大多都是 Ctrl + V,浪费俺的宝贵时间 现在我总结配出一套,给大家参考参考,可能有不足,请大家多多交流。 附:内有弹出...
4.8.2 使用BeanFactoryPostProcessor 自定义配置元数据. 81 4.8.2.1 示例:PropertyPlaceholderConfigurer. 82 4.8.2.2 示例:PropertyOverrideConfigurer 83 4.8.3 使用FactoryBean 来自定义实例化...
-- 当配置文件修改后,系统自动加载该文件。开发阶段建议打开此功能 --> <!-- 指定浏览器输出的编码格式 --> <!--将action内容放在package元素下,package元素的name值与extends值可以使用默认值,如下所...
什么 与通过从属性文件读取其配置来进行自我配置相比,对于应用程序而言,有什么可能更简单? 好吧,对于业务应用程序而言... 首先,我们将使用PropertyPlaceholderConfigurer通过读取存储在.properties文件中的属性来
spring3.1中文参考文档,南磊翻译,现在有4章,目录如下: 第一部分 Spring framework概述.......................................................................................................................
PropertyPlaceholderConfigurer PropertyResourceConfigurer PropertyValue PropertyValues PropertyValuesEditor PrototypeAspectInstanceFactory PrototypeTargetSource ProxyConfig ProxyFactory ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
JDBCTemp+MySQL示例。Spring,PropertyPlaceholderConfigurer,BeanPropertyRowMapper