CodeInventor Group程序员@IT·互联网

Spring配置文件中读取properties加密信息

2017-02-08  本文已影响1415人  瑶瑶小仙女

昨天遇到的一个Spring配置文件中读取properties配置中的配置信息,那么咱们有没有想到是不是可能配置中配置信息其实是加密的,哈哈,完全有可能的哦。今天咱们就来看一下Spring读取properties中的配置的加密配置信息。
目标:咱们的一些配置项咱们想根据不同的环境配置不同的配置,还有就是这些配置可能比较重要我们不太想别人一下就看出来,配置文件properties中我们直接放的是加密信息,咱们就需要在不同的环境中读取不同的加密信息。
好了,还是昨天的思路,我们看过昨天的Spring中采用占位符${} 获取配置的方法,就是采用这样的做法,但是我们必须采用自己的处理类。具体的做法就是我们自己新建一个类继承PropertyResourceConfigurer,PropertyResourceConfigurer继承了PropertiesLoadeSupport类,咱们细心的同学昨天已经跟进去源码看过了,这个就是spring提供的元素继承的父类,咱们也可以这么干,自定义咱们的配置读取方式。废话不说上代码:
<pre>
package com.spring.traning.service;

import com.spring.traning.util.DESUtil;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.List;

public class MyPropertyPlaceholder extends PropertyPlaceholderConfigurer {

private List<String> encryptPropNames;

@Override
protected String convertProperty(String propertyName, String propertyValue) {

    if (encryptPropNames.contains(propertyName)) {
        /**
         * 在这里做一些 加解密 操作
         */
        return DESUtil.decrypt(propertyValue);
    }

    return super.convertProperty(propertyName, propertyValue);
}

public List<String> getEncryptPropNames() {
    return encryptPropNames;
}

public void setEncryptPropNames(List<String> encryptPropNames) {
    this.encryptPropNames = encryptPropNames;
}

}
</pre>
这个就是咱们自定义的Property处理类继承自PropertyPlaceholderConfigurer,然后重写PropertyPlaceholderConfigurer的父类的PropertyResourceConfigurer中的方法convertProperty,这里面有两个重载的方法,protected void convertProperties(Properties props)和protected String convertProperty(String propertyName, String propertyValue)看注释就是第一个是返回所有的配置文件的中的配置,第二个是返回对应的配置,咱们使用的就是第二个方法,返回对应的加密的配置信息。首先看下源码的实现:
<pre>
protected String convertProperty(String propertyName, String propertyValue) {
return convertPropertyValue(propertyValue);
}
</pre>
<pre>
protected String convertPropertyValue(String originalValue) {
return originalValue;
}
</pre>
很简单哈,直接返回值参数中的propertyValue,没有做任何的处理,哎别急还有一个propertyName呢,咱们在这个上面做文章,就是上面咱们贴的代码了,根据propertyName选择出咱们需要进行特殊处理的propertyVale处理就行:
<pre>
if (encryptPropNames.contains(propertyName)) {
// 在这里做一些 加解密 操作
return DESUtil.decrypt(propertyValue);
}
</pre>
这里就是主要的处理了,简单吧。下面把base64的代码贴一下:
<pre>
package com.spring.traning.util;

import org.springframework.util.Base64Utils;

import java.io.UnsupportedEncodingException;

public class DESUtil {
public static String encrypt(String src) {
try {
return Base64Utils.encodeToString(src.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

    return null;
}

public static String decrypt(String src) {
    try {
        return new String(Base64Utils.decodeFromString(src), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return null;
}


public static void main(String[] args) {
    System.out.println(encrypt("aty"));
    System.out.println(encrypt("123456"));
}

}

</pre>
下面就是使用我们的配置信息,需要注意的是我们的配置需要配置出我们自定义的属性
<pre>
private List<String> encryptPropNames;
</pre>
添加成我们执行的需要特殊处理的propertyName的集合中:
<pre>
<bean id="propertyConfigurer"
class="com.spring.traning.service.MyPropertyPlaceholder">
<property name="encryptPropNames">
<list>
<value>databases_username_after_entry</value>
<value>databases_password_after_entry</value>
</list>
</property>
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>

<bean id="dataBasesSource" class="com.spring.traning.service.DataBasesSource">
    <property name="userName" value="${databases_username_after_entry}"/>
    <property name="passWord" value="${databases_password_after_entry}"/>
</bean>

</pre>
然后就是我们配置文件和测试bean的定义:
<pre>
package com.spring.traning.service;

public class DataBasesSource {
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;
}

}
</pre>
配置我也是直接加在了:resource目录下



最后是测试test:
<pre>
public class APPTest {
public static void main(String[] args) {
ApplicationContext applicationContext = new FileSystemXmlApplicationContext(new String[] {"classpath:applicationContext.xml"});
DataBasesSource dataBasesSource = (DataBasesSource) applicationContext.getBean("dataBasesSource");

    System.out.println(dataBasesSource.getUserName() + " " + dataBasesSource.getPassWord());
}

}
</pre>
好啦,到此咱们就掌握了这门技术点了,spring中读取properties中的配置,加密配置。

上一篇 下一篇

猜你喜欢

热点阅读