我做了一个系统,做完了,我们经理要我把数据库里面的dbconfig.properties文件加密,#Database Settings
#db url
DATABASE_URL=10.1.3.220
 #222.73.31.69
#db name
DATABASE_NAME=rwcm
 
#db login user name
USERNAME=A8E4667ADA143D75B331E012FB568718
 
#db login user password
PASSWORD=987A6A3F7EB623CA04B113B7177952EC
 
#db port
DATABASE_PORT=1521
driverClassName=oracle.jdbc.driver.OracleDriver
上面的是配置访问数据库的帐号密码,我写的密文,我另外写了一个解密类,但是我解密了里面然后返回出来
public class MyConfigurer extends PropertyPlaceholderConfigurer{  public MyConfigurer(){
processProperties(); 
//PropertyPlaceholderConfigurer.SYSTEM_PROPERTIES_MODE_OVERRIDE
}
protected void processProperties()  
            throws BeansException {  
Properties pro = new Properties();

try {
 InputStream in;
in= MyConfigurer.class.getResourceAsStream("/dbconfig.properties"); 
pro.load(in);;
in.close();

        String un = pro.getProperty("USERNAME");  
        String ps=pro.getProperty("PASSWORD");
        if(un!=null){
         System.out.println(AESUtil.decrypt(ps,"r20100716w"));
         pro.setProperty("USERNAME", AESUtil.decrypt(un,"r20100716w"));
         System.out.println("---已经初始化用户名");
         pro.setProperty("PASSWORD", AESUtil.decrypt(ps,"r20100716w"));
         System.out.println("---已经初始化密码");
          FileOutputStream fos=new FileOutputStream("/dbconfig.properties");
          pro.store(fos,"");
          fos.close();
        }
} catch (Exception e) {
e.printStackTrace();
}
}
我继承了spring的PropertyPlaceholderConfigurer这个类,为什么我启动tomcat去访问数据库提示帐号活密码错误,登录拒绝,所以想问问大家怎么替换那些密文?我需要别人看那个properties文件的时候里面是密文,但是需要操作数据库的时候拿的是解密了的密码和帐号,这个怎么搞?求告诉解决

解决方案 »

  1.   

    in= MyConfigurer.class.getResourceAsStream("/dbconfig.properties"); 
    这里取数据把in的数据改掉就可以了~
      

  2.   

    是的,我里面写的密文可以解密出来,但是spring还是回去那个文件里面读取未解析出来的密文,这个怎么做?
      

  3.   

    这应该不是解密没配置好的原因吧,我是用了上面那个继承了spring方面的那个类调用的,可以解析出密码,我都装载进去了,可spring就是不去读取,我也不知道咋办了
      

  4.   

    重写?这个。loadproperties是哪个的方法?我new了一个properties了
      

  5.   

    <bean id="propertyConfig"
              class="com.bean.DecryptPropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:system.properties</value>
                </list>
    public class DecryptPropertyPlaceholderConfigurer
            extends PropertyPlaceholderConfigurer {
        private Resource[] locations;   ① 重新定义父类中的这个同名属性
        private Resource keyLocation; ② 用于指定密钥文件
        public void setKeyLocation(Resource keyLocation) {
            this.keyLocation = keyLocation;
        }
        public void setLocations(Resource[] locations) {
            this.locations = locations;
        }
        public void loadProperties(Properties props) throws IOException {
            if (this.locations != null) {
                PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
                for (int i = 0; i < this.locations.length; i++) {
                    Resource location = this.locations[i];
                    if (logger.isInfoEnabled()) {
                        logger.info("Loading properties file from " + location);
                    }
                    InputStream is = null;
                    try {
                        is = location.getInputStream();
                            ③ 加载密钥
                        Key key = DESEncryptUtil.getKey(keyLocation.getInputStream());
                        ④ 对属性文件进行解密
    is = DESEncryptUtil.doDecrypt(key, is);
    ⑤ 将解密后的属性流装载到props中
                        if(fileEncoding != null){
                            propertiesPersister.load(props,
            new InputStreamReader(is,fileEncoding));
                        }else{
                            propertiesPersister.load(props ,is);
                        }
                    } finally {
                        if (is != null)
                            is.close();
                    }
                }
            }
        }
        }
    }
      

  6.   

    你这个不用得到properties文件?
      

  7.   

    你难道需要得到那个properties文件?