PropertyPlaceholderConfigurer扩展,实现数据库密码加密


/**
 * yingyinglicai.com Inc.
 * Copyright (c) 2013-2013 All Rights Reserved.
 */
package com.yylc.platform.common.xml.support;

import java.text.MessageFormat;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.yylc.common.lang.StringUtils;
import com.yylc.platform.common.configration.ConfigrationFactory;
import com.yylc.platform.common.security.AesCryptoHelper;
import com.yylc.platform.common.utils.LogUtil;

/**
 * 增加xml动态变量替换支持.
 * <pre>
 *      1. 优先系统统配system_config获取变量.
 *      2. 变量不存在,则从系统环境变量中获取.
 *      3. 从自定义环境中获取.
 * </pre>
 * 
 */
public class XmlPropertyPlaceholderSupport extends PropertyPlaceholderConfigurer {

    /** 搜索系统变量开关 */
    private boolean searchSystemEnvironment = true;

    /** 搜索system_config变量开关 */
    private boolean searchSysConfig         = true;

    /**
     * 变量替换顺序调整
     * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#resolvePlaceholder(java.lang.String, java.util.Properties, int)
     */
    @Override
    protected String resolvePlaceholder(String placeholder, Properties props,
                                        int systemPropertiesMode) {
        String propVal = null;
        if (systemPropertiesMode == SYSTEM_PROPERTIES_MODE_OVERRIDE) {
            propVal = resolveSystemProperty(placeholder);
        }
        if (propVal == null) {
            propVal = resolvePlaceholder(placeholder, props);
        }
        if (propVal == null && systemPropertiesMode == SYSTEM_PROPERTIES_MODE_FALLBACK) {
            propVal = resolveSystemProperty(placeholder);
        }
        // 针对密码DES解密处理
        if (StringUtils.contains(placeholder, "password")) {
            propVal = Security.decryptAES(propVal);
        }
        return propVal;
    }

    /**
     * 覆盖框架默认加载顺序
     * @see org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#resolveSystemProperty(java.lang.String)
     */
    @Override
    protected String resolveSystemProperty(String key) {
        try {
            String value = StringUtils.EMPTY_STRING;
            //【1】 优先system_config文件搜索
            if (searchSysConfig) {
                value = ConfigrationFactory.getConfigration().getPropertyValue(key);
            }

            //【2】 从系统环境变量中搜索
            if (StringUtils.isBlank(value) && this.searchSystemEnvironment) {
                value = System.getProperty(key);
            }

            //【3】 从自定义环境变量中搜索
            if (StringUtils.isBlank(value) && this.searchSystemEnvironment) {
                value = System.getenv(key);
            }

            return value;
        } catch (Throwable ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Could not access system property '" + key + "': " + ex);
            }
            return null;
        }
    }

    public void setSearchSystemEnvironment(boolean searchSystemEnvironment) {
        this.searchSystemEnvironment = searchSystemEnvironment;
    }

    public void setSearchSysConfig(boolean searchSysConfig) {
        this.searchSysConfig = searchSysConfig;
    }

    static class Security {
        private static final Logger logger = Logger.getLogger(Security.class);

        public static String decryptAES(String str) {
            try {
                if (StringUtils.length(str) <= 30) {
                    return str;
                }
                str = AesCryptoHelper.decrypt("yylc8888", str);
            } catch (Exception e) {
                LogUtil.error(logger, MessageFormat.format("密码解密失败, text={0}", str));
            }
            return str;
        }
    }
}

在生产为了保护密码不可见,需要对明文的密码加密

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.5.161:3306/entrustmer?useUnicode=true&characterEncoding=gbk
user=wuhao

password=wuhao1234

可以重写PropertyPlaceholderConfigurer

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://192.168.15.230:3306/entrustmer?useUnicode=true&characterEncoding=gbk
user=wuhao
password=5A5FC8A5752A45371ECEF616BA7FD0F1



发布了20 篇原创文章 · 获赞 23 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/hdu09075340/article/details/79445175