我写了一个解密类读取properties里面的数据库帐号和密码然后解密,为什么在需要操作数据库的时候,拿的还是我加密的密文,而没经过解密的方法了?有什么办法可以让spring去拿的是解密后的帐号密码,而不是密文 

解决方案 »

  1.   

    楼主请尝试下继承PropertyPlaceholderConfigurer
    然后覆写以下方法,应该可以解决你的问题
    protected String convertPropertyValue(String originalValue) 
    {
    return originalValue;
    }
    Convert the given property value from the properties source to the value that should be applied.
    The default implementation simply returns the original value. Can be overridden in subclasses, for example to detect encrypted values and decrypt them accordingly.
      

  2.   

    application.xml<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <bean name="" class="com.zhangyz.www.spring.MinePropertyPlaceholderConfigurer">
    <property name="location">
                <value>classpath:com/zhangyz/www/spring/jdbc.properties</value>
            </property>
    </bean>
    <bean name="test" class="com.zhangyz.www.spring.Test">
    <property name="userName" value="${test.userName}">
    </property>
    <property name="password" value="${test.password}">
    </property>
    </bean>
    </beans>Java类package com.zhangyz.www.spring;import org.apache.log4j.Logger;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {
    private String userName;
    private String password;

    public String getUserName() {
    return userName;
    } public void setUserName(String userName) {
    this.userName = userName;
    } public String getPassword() {
    return password;
    } public void setPassword(String password) {
    this.password = password;
    } private static Logger logger = Logger.getLogger(Test.class.getName()); /**
     * @param args
     */
    public static void main(String[] args) {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
    "/com/zhangyz/www/spring/application.xml");
    Test test=(Test) applicationContext.getBean("test");
    logger.error(test.getUserName());
    logger.error(test.getPassword());
    }
    }
    package com.zhangyz.www.spring;import org.apache.log4j.Logger;
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;public class MinePropertyPlaceholderConfigurer extends
    PropertyPlaceholderConfigurer {
    private static Logger logger = Logger
    .getLogger(MinePropertyPlaceholderConfigurer.class.getName()); protected String convertPropertyValue(String originalValue) {
    return originalValue+"哥哥来解密!";
    }}properties文件:test.userName=121313132132
    test.password=pppppppppppp
      

  3.   

    你竟然测试了,那怎么和我的结果不一样哩,明明可行的嘛...
    2011-11-24 13:26:21,750 ERROR [main - (Test.java:36) - com.zhangyz.www.spring.Test] 121313132132哥哥来解密!
    2011-11-24 13:26:21,750 ERROR [main - (Test.java:37) - com.zhangyz.www.spring.Test] pppppppppppp哥哥来解密!
    这是输出结果,你在那个地方处理下加密解密不就可以了
      

  4.   

    俺,估计你又要问详细的加密解密方法了,那就贴个全的吧,不说方案了说的蛋疼,再帮不上你,俺也无法了,这是你的第二篇帖子了
    properties文件:test.userName=121313132132
    test.password=after-encrypt:ppppppppppppjava文件package com.zhangyz.www.spring;import org.apache.log4j.Logger;
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;public class MinePropertyPlaceholderConfigurer extends
    PropertyPlaceholderConfigurer {
    private String encrypt="after-encrypt:";
    private static Logger logger = Logger
    .getLogger(MinePropertyPlaceholderConfigurer.class.getName()); protected String convertPropertyValue(String originalValue) {
    if(originalValue!=null&&originalValue.length()>encrypt.length()&&originalValue.indexOf(encrypt)>=0){
    return originalValue.substring(encrypt.length());
    }else{
    return originalValue;
    }
    }}
      

  5.   

    [Quote=引用 7 楼 zyz1985 的回复:]<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="" class="com.zhangyz.www.spring.MinePropertyPlaceholderConfigurer"> <property name="location"> <value>classpath:com/zhangyz/www/spring/jdbc.properties</value> </property> </bean> <bean name="test" class="com.zhangyz.www.spring.Test"> <property name="userName" value="${test.userName}"> </property> <property name="password" value="${test.password}"> </property> </bean> </beans> 
    [Quote]
    你直接这样改spring的配置你觉得spring可以识别?
      

  6.   

    研究了一下  点出你的问题  重写方法 convertProperties(Properties pro)   处理完加密解密后 将pro.store(fos,"jdbc.username"); pro.store(fos,"jdbc.password");  完成!
      

  7.   

    提供个比较使用的读取某种命名类型的properties文件都Map中:
    使用:PropertiesUtils.getAll();package com.commons;import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Properties;import org.springframework.core.io.Resource;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.core.io.support.ResourcePatternResolver;public class PropertiesUtils {
        private static ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
        private static final String DEFAULT_RESOURCE_PATTERN = "classpath:*-tag.properties";
        private static final Map<Object, Object> all = new HashMap<Object, Object>();    public static Map<Object, Object> getAll() {
    return all;
        }    static {
    try {
        Resource[] resources = resourcePatternResolver
        .getResources(DEFAULT_RESOURCE_PATTERN);
        if (resources != null) {
    for (Resource r : resources) {
        Reader in = new InputStreamReader(r.getInputStream(),
        "UTF-8");
        Properties p = new Properties();
        p.load(in);
        all.putAll(p);
        in.close();
    }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
        }}
      

  8.   

    一般账号密码都是直接写在spring配置文件里的,反正用户又看不到。。