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<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.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">
        <!--加载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]

测试代码下载

代码下载

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值