Properties配置文件数据加密

在一些情况下,需要对properties中的配置数据进行加密。比如Mysql数据库的密码,不应该以明文的形式直接保存在properties中,应该以密文的形式保存.

PropertiesUtil

用于读取properties配置文件的数据,并且将加密的密文进行解密.

/**
 * Parsing The Perperties Configuration File
 */
public final class PropertiesUtil extends PropertyPlaceholderConfigurer {
    private static final byte[] KEY = { -81, 0, 105, 7, -32, 26, -49, 88 };
    private static Map ctxPropertiesMap; // 用于存放perperties中的数据
    private List decryptProperties; // 用于存放需要解密的key

    @Override
    protected void loadProperties(Properties props) throws IOException {
        super.loadProperties(props);
        ctxPropertiesMap = new HashMap();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            if (decryptProperties != null && decryptProperties.contains(keyStr)) {
                value = SecurityUtil.decryptDes(value); // 解密
                props.setProperty(keyStr, value); // 设置解密后的明文数据
            }
            ctxPropertiesMap.put(keyStr, value);
        }
    }

    /**
     * @param decryptPropertiesMap
     *            the decryptPropertiesMap to set
     */
    public void setDecryptProperties(List decryptProperties) {
        this.decryptProperties = decryptProperties;
    }

    /**
     * Get a value based on key , if key does not exist , null is returned
     * 
     * @param key
     * @return
     */
    public static String getString(String key) {
        try {
            return ctxPropertiesMap.get(key);
        } catch (MissingResourceException e) {
            return null;
        }
    }

    /**
     * 根据key获取值
     * 
     * @param key
     * @return
     */
    public static int getInt(String key) {
        return Integer.parseInt(ctxPropertiesMap.get(key));
    }

    /**
     * 根据key获取值
     * 
     * @param key
     * @param defaultValue
     * @return
     */
    public static int getInt(String key, int defaultValue) {
        String value = ctxPropertiesMap.get(key);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return Integer.parseInt(value);
    }

    /**
     * 根据key获取值
     * 
     * @param key
     * @param defaultValue
     * @return
     */
    public static boolean getBoolean(String key, boolean defaultValue) {
        String value = ctxPropertiesMap.get(key);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return new Boolean(value);
    }

    public static void main(String[] args) {
        String encrypt = SecurityUtil.encryptDes("12345", KEY);
        System.out.println(encrypt);
        System.out.println(SecurityUtil.decryptDes(encrypt, KEY));
    }
}

jdbc.properties

db.driver=com.mysql.jdbc.Driver
db.reader.url=jdbc:mysql://127.0.0.1:3306/mydatabase
db.reader.username=root
db.reader.password=siUZ0QNpCcq=

spring中的配置文件


    <bean class="com.spring.util.PropertiesUtil">
        
        <property name="locations">
            <list>
                <value>classpath:config/jdbc.propertiesvalue>
            list>
        property>

        
        <property name="decryptProperties">
            <array>
                
                <value>db.reader.passwordvalue>
            array>
        property>
    bean>

    
    <bean id="connectionConfig" class="com.spring.bean.ConnectionConfig">
        <property name="password" value="${db.reader.password}" />
    bean>

输出的密码,是解密后的明文

测试代码

public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-*.xml");
        System.out.println(applicationContext.getBean("connectionConfig"));
}

测试输出结果

ConnectionConfig [password=12345]

测试代码下载

代码下载

你可能感兴趣的:(Spring,Util)