`

加密Spring加载的Properties文件

 
阅读更多
实现思路:重写加载器的方法,做到偷梁换柱,在真正使用配置之前完成解密。
一,编写工具类,继承PropertyPlaceholderConfigurer
package com.csair.cbd.utils;

import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import com.csair.cbd.member.utils.CryptUtils;
import com.csair.common.exceptions.ServiceException;

/**
 * 配置文件解密类
 * @author liuyouyi
 * @company csair
 * 2013-10-21
 * sybase
 * com.csair.cbd.utils
 * @version 1.0
 *
 */
public class MyConfigurer extends PropertyPlaceholderConfigurer {  
	
	public final static String strKey = "4321aaaaaaaa1314aaaaalyy";
	private static final Log logger = LogFactory.getLog(MyConfigurer.class);
  
    @Override  
    protected void processProperties(  
            ConfigurableListableBeanFactory beanFactory, Properties props)  
            throws BeansException {  
  
       
        
        String jdbcPassword = props.getProperty("jdbc.password");
        if(jdbcPassword != null ) {
        	try {
        		//logger.info("----------------------------加密前-----------------------------");
				props.setProperty("jdbc.password", decrypt(jdbcPassword));
				//logger.info("----------------------------加密后-----------------------------");
			} catch (Exception e) {				
			    e.printStackTrace();				
			}
        }
       String jdbc2Password = props.getProperty("jdbc2.password");
        if(jdbc2Password != null ) {
        	try {
				props.setProperty("jdbc2.password", decrypt(jdbc2Password));
			} catch (Exception e) {				
			    e.printStackTrace();				
			}
        }
        
        String iqjdbcPassword = props.getProperty("iqjdbc.password");
        if(iqjdbcPassword != null ) {
        	try {
				props.setProperty("iqjdbc.password", decrypt(iqjdbcPassword));
			} catch (Exception e) {				
			    e.printStackTrace();				
			}
        }
        
        String jdbcMysqlPassword = props.getProperty("jdbc.mysql.password");
        if(jdbcMysqlPassword != null ) {
        	try {
				props.setProperty("jdbc.mysql.password", decrypt(jdbcMysqlPassword));
			} catch (Exception e) {				
			    e.printStackTrace();				
			}
        }
       
        String mysqlJdbcPassword = props.getProperty("mysql.jdbc.password");
        if(mysqlJdbcPassword != null ) {
        	try {
				props.setProperty("mysql.jdbc.password", decrypt(mysqlJdbcPassword));
			} catch (Exception e) {				
			    e.printStackTrace();				
			}
        }
        
        super.processProperties(beanFactory, props);       
  
    }  
    
    /**
	 * 加密
	 * 
	 * @param password
	 */
	public static String encrypt(String password) throws Exception {

		// SHA1加密
		String uid = CryptUtils.getInstance(CryptUtils.Algorithm_DESede)
				.encrypt(password, strKey);
		return uid;
	}

	/**
	 * 解密
	 * 
	 * @param password
	 */
	public static String decrypt(String password) throws ServiceException {
		try {
			return CryptUtils.getInstance(CryptUtils.Algorithm_DESede).decrypt(
					password, strKey);
		} catch (Exception e) {
			throw new ServiceException("解密失败!");
		}
	}
	
	
	public static void main(String[] args) {
		try {
			String string1 = encrypt("test_password");
			String string2 = decrypt(string1);
			//String string3 = ReserverCookie.decode(string2);
			System.out.println(string1+"--"+string2);			
		} catch (Exception e) {
		}
	}
    
}  



二,改写配置文件
<bean id="JDBCpropertyConfigurer"  
        class="com.csair.cbd.utils.MyConfigurer">  
        <property name="ignoreResourceNotFound" value="true" />  
        <property name="locations">  
            <list>  
                <value>classpath:jdbc.properties</value> 
				<value>classpath:system.properties</value>
				<value>classpath:sms-template.properties</value>
				<value>classpath:deploy/test.properties</value>
				<value>classpath:com/csair/cbd/member/member-verify.properties</value>
				<value>classpath:com/csair/cbd/rule/rule-message.properties</value> 
				<value>classpath:com/csair/cbd/integral/services/impl/deferregist-message.properties</value>
            </list>  
        </property>  
    </bean>  


三,整合其它的PropertyPlaceholderConfigurer配置,以免被覆盖
四,去掉context:property-placeholder,否则配置也是会被覆盖的
<!--context:property-placeholder location="classpath:jdbc.properties,classpath:system.properties,classpath:sms-template.properties,classpath:deploy/test.properties,classpath:com/csair/cbd/member/member-verify.properties,classpath:com/csair/cbd/rule/rule-message.properties"/-->
   	

分享到:
评论

相关推荐

    spring读取properties

    在Spring框架中,读取和使用Properties文件是一种常见的配置方式,尤其当涉及到数据库连接、环境变量等需要在运行时动态加载或更改的信息时。本文将深入解析如何在Spring环境中读取Properties文件,包括配置步骤、...

    spring中properties加密

    本文将深入探讨如何在Spring项目中实现properties文件的加密。 1. **理解Properties文件** Spring框架广泛使用`properties`文件来存储配置信息。这些文件通常是纯文本格式,容易被读取,从而增加了安全性风险。...

    Spring@PropertySource 配置文件自定义加密、自定义Yaml文件加载

    1. **配置加载顺序**:Spring加载配置文件时有默认的顺序,如果你同时使用了加密的.properties和YAML配置,需要合理安排加载顺序,以保证依赖关系正确。 2. **自定义`ApplicationContextInitializer`**:如果你需要...

    Spring加载加密的配置文件详解

    现在,当Spring加载`db.properties`时,`EncryptPropertyPlaceholderConfigurer`会自动解密`db.password`,并将其作为安全的密码值传递给应用程序的其他组件。 总之,通过自定义`PropertyPlaceholderConfigurer`...

    spring,配置文件从属性文件读取JDBC连接的相关参数

    在Spring配置文件(如`applicationContext.xml`或`beans.xml`)中,我们可以创建一个`Properties` bean来加载`jdbc.properties`: ```xml &lt;bean id="propertyConfigurer" class="org.springframework.beans....

    spring中的数据源配置信息加密方案

    在Spring启动时,通过配置Jasypt的解密器,使得Spring能够自动解密并加载这些加密的属性。 2. **环境变量或系统属性**:将敏感信息存储在环境变量或系统属性中,而不是直接写在配置文件里。这样,即使有人获取到...

    Spring Boot 实现配置文件加解密原理

    Spring Boot 的配置文件通常是以 Properties 文件或 YML 文件的形式存在的,而这些文件中可能包含敏感信息,例如数据库连接密码、API 密钥等。如果这些信息被泄露,将会对应用程序的安全性产生严重的影响。因此,...

    Spring配置加密方案收集.pdf

    在Spring配置中,我们可以使用`PropertyPlaceholderConfigurer`或`PropertiesFactoryBean`来加载加密后的属性文件,并在运行时调用`EncryptPropertyFile`类的解密方法,确保Spring在初始化时能够正确解析和加载加密...

    Springboot+PBEWITHHMACSHA512ANDAES-128配置文件密码加密

    在Spring Boot应用中,我们通常会有一个包含数据库连接、API密钥等敏感信息的配置文件,如application.yml或application.properties。为了保护这些信息不被未经授权的人员访问,我们需要对它们进行加密。Spring Boot...

    Spring中PropertyPlaceholderConfigurer的使用

    PropertyPlaceholderConfigurer 不仅可以用于加载 Properties 文件,还可以用于实现其他功能,例如属性文件加密解密等。在后续的文章中,我们将继续讨论这些扩展应用。 PropertyPlaceholderConfigurer 是 Spring ...

    系统核心配置项加密解决方案

    在这个特定的情况下,工厂可能负责加载加密后的配置文件,然后在运行时动态解密它们,以便应用程序可以安全地使用。 1. **加密算法选择**:在设计这样的解决方案时,选择一个安全的加密算法是关键。常见的选择包括...

    Spring Cloud Config RSA简介及使用RSA加密配置文件的方法

    引入RSA加密,我们可以将配置文件中的敏感信息加密,只在Config Server中保留私钥进行解密,这样即使配置文件被非法获取,也无法直接读取到明文信息。以下是使用RSA加密配置文件的步骤: 1. **生成密钥对**: 使用...

    Spring项目application.xml配置文件加解密

    接下来,我们需要自定义一个配置处理器,以便在Spring加载`application.xml`时自动解密配置属性。`EncryptPropertyPlaceholderConfigurer.java`就是这样一个类,它扩展了Spring的`PropertyPlaceholderConfigurer`,...

    基于jvmti 的Java 代码加密

    2. **配置启动参数**:在Tomcat的启动脚本或Spring Boot的`application.properties`中添加jvmti代理的相关参数,指定库文件路径和相关选项。 3. **测试和调试**:启动服务器,确保jvmti代理能够正确加载,并且代码...

    【SpringBoot探索三】添加配置文件参考案例

    总结,本案例旨在帮助开发者理解Spring Boot中的配置文件管理,包括加载、注入、加密等环节,同时强调了在实际项目(如`webapp-parent`)中配置文件的应用。通过学习,你将能够更好地掌控Spring Boot项目的配置,...

    spring cloud 官方文档

    2. Bootstrap Properties: 指定了应用程序上下文启动时使用的外部配置,这些配置通过外部配置源来加载。可以配置的位置以及如何覆盖远程属性值。 3. 自定义引导配置:通过Customizing the Bootstrap Configuration...

    spring boot面试题及答案.docx

    Spring Boot中有两个重要的配置文件:`application.properties` 或 `application.yml` 和 `bootstrap.properties` 或 `bootstrap.yml`。 - **application 配置文件**:主要用于Spring Boot项目的自动化配置,定义...

    Spirng Boot-_配置文件详解

    Spring Boot支持两种主要的配置文件:`application.properties`和`application.yml`。`.properties`文件采用键值对的形式,而`.yml`文件则使用更加直观的层级结构。这两种文件都可以在应用的类路径根目录下找到,...

    jdbc.properties

    5. **多环境配置**:在开发、测试和生产环境中,数据库连接信息可能不同,因此配置文件可能需要根据环境变量动态加载,这可以通过Maven的profile或Spring Boot的`application-{profile}.properties`来实现。...

    springsecurity使用demo

    - `application.properties` 或 `application.yml`:可能包含了一些配置属性,如数据库连接信息、密码加密算法等。 - `pom.xml`:Maven 项目配置文件,包含了 Spring Security 和其他依赖的版本信息。 - `webapp` ...

Global site tag (gtag.js) - Google Analytics