当前位置: 代码迷 >> ASP >> 加密properties资料,使用jaspt和spring加密
  详细解决方案

加密properties资料,使用jaspt和spring加密

热度:306   发布时间:2012-08-30 09:55:54.0
加密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文件中使用自己的加密算法,加密,然后在这个方法中添加自己的解密算法,解密。

?

  相关解决方案