如何把配置文件的数据库密码加密?求解答

图片说明
绝对不能采用明文的形式存储密码,这里谁有好的办法解决,谢谢

http://blog.csdn.net/zhangt85/article/details/42122311

自己写一个加密程序,密码 ,加密 --》 放入配置文件
,配置文件不就是读取这个值。读到之后 ,解密再代入password

刚才找了一下,大致就说下思路:
1、这个properties里面的密码是使用加密后的,也就是说不是那种我写明文,然后系统启动之后加密,在写回配置文件中。
也就是说,我们一开始就生成密文放到这个配置文件中。

2、Spring提供了一个工具类,这个类名字叫做:
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer
你继承他,然后覆盖其中的一个方法,
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)

这个方法有两个入参,第二个参数是我们重点关注的,就是properties。

spring启动的时候创建类,然后读取配置文件,配置文件中的db.username 或者db.passwd就放在这个里面,

然后你就能拿到你写在properties中的加密之后的密码了,你在解密,然后调用props.setProperty方法。

将明文覆盖掉读取的密文,记住,key必须一样。

然后就OK了。

记住,你写的这个类需要配置在spring中。

图片说明
这是我自己在网上找的,但是我的配置是这样的,这应该怎么改,谢谢
图片说明

我给你整理了一下,大致的步骤如下:
第一步:创建一个类,继承PropertyPlaceholderConfigurer

 package com.csdn;

import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public class EncryptProperties extends PropertyPlaceholderConfigurer{

    private String[] entryProperties = new String[]{"db.username", "db.password"};

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
            throws BeansException {

        for(String key : entryProperties){
            String value = props.getProperty(key);
            // 这里是解密,用户名密码前加OK,移除OK才是正确的
            props.setProperty(key, value.replaceAll("OK", ""));

            System.out.println();
        }

        super.processProperties(beanFactoryToProcess, props);
    }

}

第二步:编辑配置文件xml

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- 这个类就是你写的那个类,然后指定属性文件位置  -->
    <bean id="propertyConfigurer" class="com.csdn.EncryptProperties">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
        <property name="fileEncoding" value="utf-8" />
    </bean>

    <!-- 上面指定了占位符,下面的${}就可以用了 -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${db.driver}" />
        <property name="url" value="${db.url}" />
        <property name="username" value="${db.username}" />
        <property name="password" value="${db.password}" />
    </bean>

</beans>

第三步:jdbc.properties

 db.driver=org.postgresql.Driver
db.url=jdbc:postgresql://192.168.133.132:5432/cmsdb
#正确的密码用户名前是没有OK的,这里模拟加密
db.username=OKpostgres
db.password=OKABC1616abc

我跑了一下,可以取到数据库里面数据
有问题你就加Q:1073747506