日期:2014-05-17  浏览次数:21127 次

加密properties文件,使用jaspt和spring加密
package org.jasypt.spring3.properties;

import org.jasypt.commons.CommonUtils;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.properties.PropertyValueEncryptionUtils;
import org.jasypt.util.text.TextEncryptor;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public final class EncryptablePropertyPlaceholderConfigurer 
        extends PropertyPlaceholderConfigurer {
	/*
	 * Only one of these instances will be initialized, the other one will be
	 * null.
	 */
	private final StringEncryptor stringEncryptor;
	private final TextEncryptor textEncryptor;

	/**
	 * <p>
	 * Creates an <tt>EncryptablePropertyPlaceholderConfigurer</tt> instance
	 * which will use the passed {@link StringEncryptor} object to decrypt
	 * encrypted values.
	 * </p>
	 * 
	 * @param stringEncryptor
	 *            the {@link StringEncryptor} to be used do decrypt values. It
	 *            can not be null.
	 */
	public EncryptablePropertyPlaceholderConfigurer(
	        final StringEncryptor stringEncryptor) {
		super();
		CommonUtils.validateNotNull(stringEncryptor, "Encryptor cannot be null");
		this.stringEncryptor = stringEncryptor;
		this.textEncryptor = null;
	}

	/**
	 * <p>
	 * Creates an <tt>EncryptablePropertyPlaceholderConfigurer</tt> instance which will use the
	 * passed {@link TextEncryptor} object to decrypt encrypted values.
	 * </p>
	 * 
	 * @param textEncryptor
	 *            the {@link TextEncryptor} to be used do decrypt values. It can
	 *            not be null.
	 */
	public EncryptablePropertyPlaceholderConfigurer(final TextEncryptor textEncryptor) {
		super();
		CommonUtils.validateNotNull(textEncryptor, "Encryptor cannot be null");
		this.stringEncryptor = null;
		this.textEncryptor = textEncryptor;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.beans.factory.config.PropertyResourceConfigurer#convertPropertyValue(java.lang.String)
	 */
	@Override
	protected String convertPropertyValue(final String originalValue) {
		if (!PropertyValueEncryptionUtils.isEncryptedValue(originalValue)) {
			return originalValue;
		}
		if (this.stringEncryptor != null) {
			return PropertyValueEncryptionUtils.decrypt(originalValue,
					this.stringEncryptor);

		}
		return PropertyValueEncryptionUtils.decrypt(originalValue, this.textEncryptor);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @since 1.8
	 * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#resolveSystemProperty(java.lang.String)
	 */
	@Override
    protected String resolveSystemProperty(final String key) {
        return convertPropertyValue(super.resolveSystemProperty(key));
    }
    
}
加密核心类,重载spring的属性加载PropertyPlaceholderConfigurer类,在 convertPropertyValue(final String originalValue) 方法中解密,
protected String convertPropertyValue(final String originalValue) {
??if (!PropertyValueEncryptionUtils.isEncryptedValue(originalValue)) {
???return originalValue;
??}
??if (this.stringEncryptor != null) {
???return PropertyValueEncryptionUtils.decrypt(originalValue,
?????this.stringEncryptor);
??}
??return PropertyValueEncryptionUtils.decrypt(originalValue, this.textEncryptor);
?}

? 1、先判断是否加密过,没有加密就直接读取,不解密。

?? 2、如果加密过,调用解密方法,通过密钥解密。

?? 3、jasypt的密钥可以通过属性文件传入,应该可以动态生成。

? 其实你可以在现在properties文件中使用自己的加密算法,加密,然后在这个方法中添加自己的解密算法,解密。

?