Properties配置文件数据加密

来源:互联网 发布:大数据与经济发展 编辑:程序博客网 时间:2024/06/08 04:05

在一些情况下,需要对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<String, String> ctxPropertiesMap; // 用于存放perperties中的数据    private List<String> decryptProperties; // 用于存放需要解密的key    @Override    protected void loadProperties(Properties props) throws IOException {        super.loadProperties(props);        ctxPropertiesMap = new HashMap<String, String>();        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<String> 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.Driverdb.reader.url=jdbc:mysql://127.0.0.1:3306/mydatabasedb.reader.username=rootdb.reader.password=siUZ0QNpCcq=

spring中的配置文件

<!-- 引入属性配置文件 -->    <bean class="com.spring.util.PropertiesUtil">        <!--加载properties配置文件 -->        <property name="locations">            <list>                <value>classpath:config/jdbc.properties</value>            </list>        </property>        <!--需要进行解密的数据对 -->        <property name="decryptProperties">            <array>                <!-- 需要解密的配置 -->                <value>db.reader.password</value>            </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]

测试代码下载

代码下载

原创粉丝点击