步骤1、jdbc.properties文件中配置用户名、密码等
jdbc.url=jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc.username=xxxx
jdbc.password=yyyy
步骤2、spring.xml中配置资源文件
方法一,使用明文用户名和密码时,直接配置文件位置:
spring.xml
方法二,spring中配置并添加一个类继承PropertyPlaceholderConfigurer实现使用加密字符串,使用时解密
spring.xml
classpath:jdbc.properties
jdbc.username
jdbc.password
jdbc.url
EncryptPropertyPlaceholderConfigurer.java
package xxx.xxx;
import java.util.Set;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
* 继承自spring的PropertyPlaceholderConfigurer来扩展
*
*/
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
private Set encryptedProps;
/** 需要解密的字段 */
private String[] encryptPropNames = { "jdbc.username", "jdbc.password" };
protected String convertProperty(String propertyName, String propertyValue) {
if (isEncryptProp(propertyName)) {
// 这里进行解密(根据实际内容改成使用的解密方法)
return Util.xxxx(propertyValue);
} else {
return propertyValue;
}
}
/**
* 判断属性是否需要解密
*
* @param propertyName
* @return
*/
@SuppressWarnings("deprecation")
private boolean isEncryptProp(String propertyName) {
for (String encryptpropertyName : encryptPropNames) {
if (ObjectUtils.equals(encryptpropertyName, propertyName)) {
return true;
}
}
return false;
}
public Set getEncryptedProps() {
return encryptedProps;
}
public void setEncryptedProps(Set encryptedProps) {
this.encryptedProps = encryptedProps;
}
}
标签:jdbc,return,String,数据源,propertyName,spring,JAVA,encryptedProps
来源: https://www.cnblogs.com/leonlipfsj/p/12256025.html