SPRING 数据库密码加密存储 在配置文件的两种方式 下一篇第二种方式

分析

SPRING通过

1.property-placeholder spring3.1以前实现是PropertyPlaceholderConfigurer,3.1以后是PropertySourcesPlaceholderConfigurer

<context:property-placeholder local-override="true" properties-ref="dataSourceProperties"
 file-encoding="UTF-8" location="classpath:loc/config.properties"
 ignore-resource-not-found="true" />

2 3.1后PropertySourcesPlaceholderConfigurer支持加载通配符*

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer" p:location="classpath*:loc/*.properties"></bean> 

按ctrl点击标签可以看到spring-context.xsd中的详细信息。

PropertySourcesPlaceholderConfigurer类经过一系列继承关系,实质是一个容器后管理器。debug走里面的一段代码如下:

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
 if (this.propertySources == null) {
 this.propertySources = new MutablePropertySources();
 if (this.environment != null) {
 this.propertySources.addLast(new PropertySource<Environment>("environmentProperties", this.environment) {
 @Nullable
 public String getProperty(String key) {
 return ((Environment)this.source).getProperty(key);
 }
 });
 }

 try {
 PropertySource<?> localPropertySource = new PropertiesPropertySource("localProperties", this.mergeProperties());
 if (this.localOverride) {
 this.propertySources.addFirst(localPropertySource);
 } else {
 this.propertySources.addLast(localPropertySource);
 }
 } catch (IOException var3) {
 throw new BeanInitializationException("Could not load properties", var3);
 }
 }

 this.processProperties(beanFactory, (ConfigurablePropertyResolver)(new PropertySourcesPropertyResolver(this.propertySources)));
 this.appliedPropertySources = this.propertySources;
 }

进入这个mergeProperties()方法里面看:

 protected Properties mergeProperties() throws IOException {
 Properties result = new Properties();
 if (this.localOverride) {
 this.loadProperties(result);
 }

 if (this.localProperties != null) {
 Properties[] var2 = this.localProperties;
 int var3 = var2.length;

 for(int var4 = 0; var4 < var3; ++var4) {
 Properties localProp = var2[var4];
 CollectionUtils.mergePropertiesIntoMap(localProp, result);
 }
 }

 if (!this.localOverride) {
 this.loadProperties(result);
 }

 return result;
 }

可以看到localOverride这个属性是用来控制是否覆盖Spring读取的属性配置。并且下面紧跟着判断this.localProperties!=null,如果localProperties不为null的话,会读取这些配置信息到spring容易中并且覆盖spring中已存在的属性。

<!-- 这里的local-override="true" 就是覆盖spring容器中已存在的属性,properties-ref="dataSourceProperties" 是指定自己的properties -->
 <context:property-placeholder local-override="true" properties-ref="dataSourceProperties"
 file-encoding="UTF-8" location="classpath:loc/config.properties"
 ignore-resource-not-found="true" />
 <!-- 这个类是我自定义的,用来解密jdbc.properties中的属性之后然后存放到Properties类中 -->
 <bean id="dataSourceProperties" class="com.spring.demo.utils.DataSourceProperties">
 <constructor-arg value="encrypt.jdbc.password"/>
 </bean>

下面是我的DataSourceProperties类


package com.spring.demo.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Properties;

/**
 * 数据源配置参数处理
 * <p/>
 * 配置信息事先被DES加密处理,需要在此解密然后绑定到数据源
 * Created by Alvin on 2016/7/31.
 */
public class DataSourceProperties extends Properties {
 protected static final Logger logger = LoggerFactory.getLogger(AesUtils.class);
 /**
 * 构造方法
 * @param propertyNames 需要解密的属性名称
 */
 public DataSourceProperties(String[] propertyNames) {
 try {
 this.load(DataSourceProperties.class.getClassLoader()
 .getResourceAsStream("loc/config.properties"));
 for (String propertyName : propertyNames) {
 decrypt(propertyName);
 }
 } catch (IOException e) {
 logger.error(e.getMessage(),e);
 }
 }

 /**
 * 解密
 */
 private void decrypt(String propertyName) {
 logger.info("propertyName({}),value({})",propertyName,this.getProperty(propertyName));
 if (isEncryptPropertyVal(propertyName)){
 String value = AesUtils.decrypt(this.getProperty(propertyName), AesUtils.key);
 logger.info("propertyName({}),propertyValue({}),",propertyName,value);
 this.setProperty(propertyName, value);
 }
 }

 /**
 * 判断属性值是否需要解密,这里我约定需要解密的属性名用encrypt开头
 * @param propertyName
 * @return
 */
 private boolean isEncryptPropertyVal(String propertyName){
 if(propertyName.indexOf("encrypt") >= 0){
 return true;
 }else{
 return false;
 }
 }

}
时间: 2024-08-03 16:16:38

SPRING 数据库密码加密存储 在配置文件的两种方式 下一篇第二种方式的相关文章

SPRING 数据库密码加密存储 在配置文件的两种方式 第一种

package com.spring.demo.utils; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.core.io.Resource; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.spri

SPRING 数据库密码加密存储 在配置文件的两种方式 第二种

spring <context:property-placeholder local-override="true" properties-ref="dataSourceProperties" file-encoding="UTF-8" location="classpath:loc/config.properties" ignore-resource-not-found="true" /> 3

hibernate配置文件中数据库密码加密

问题描述 hibernate配置文件中数据库密码加密 求大神支招啊... 这是的hibernate.xml <?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-co

问个关于aspx网站数据库密码加密解密的问题

问题描述 333222310110410396919810307对应的明文密码为longbin3322222222110112454247464338404410对应的明文密码为xz74985026这是一种什么加密方式啊如何解密http://51.dc.ftn.qq.com/ftn_handler/fb1e7b2d252d2424bf6f3740d11f1fc8d27a82f6826b2b57904e7e5872b8bdb3e72380aee697bf93cf493f337530cf2ae315

Spring Security笔记:使用BCrypt算法加密存储登录密码

在前一节使用数据库进行用户认证(form login using database)里,我们学习了如何把"登录帐号.密码"存储在db中,但是密码都是明文存储的,显然不太讲究.这一节将学习如何使用spring security3新加入的bcrypt算法,将登录加密存储到db中,并正常通过验证. 一.Bcrypt算法 1 int t = 0; 2 String password = "123456"; 3 System.out.println(password + &q

使用 Salt + Hash 将密码加密后再存储进数据库_实用技巧

(一) 为什么要用哈希函数来加密密码 如果你需要保存密码(比如网站用户的密码),你要考虑如何保护这些密码数据,象下面那样直接将密码写入数据库中是极不安全的,因为任何可以打开数据库的人,都将可以直接看到这些密码. 解决的办法是将密码加密后再存储进数据库,比较常用的加密方法是使用哈希函数(Hash Function).哈希函数的具体定义,大家可以在网上或者相关书籍中查阅到,简单地说,它的特性如下: (1)原始密码经哈希函数计算后得到一个哈希值 (2)改变原始密码,哈希函数计算出的哈希值也会相应改变

OGG 安全特性:加密数据库密码

二.加密数据库密码 可以通过GoldenGate加密一些数据库口令,可以加密的数据库口令大致有下列3种. GoldenGate Extract.Replicat进程及其他进程登录到数据库的密码. ASM数据库.GoldenGate需要登录到ASM实例的密码. GoldenGate开启DDL的情况下,如果生产端执行类似CREATE | ALTER} USER <name> IDENTIFIED BY <password> 的操作,容灾端有参数DDLOPTIONS DEFAULTUSE

xml加密-spring 在读取加密后的xml配置文件的时候,怎么spring先解密再读取

问题描述 spring 在读取加密后的xml配置文件的时候,怎么spring先解密再读取 小弟新手求各位大神帮帮忙======================= 解决方案 首先要把加密配置文件先单独解密成明文,然后把明文配置文件拷贝到项目的类路径下,修正spring的配置信息. 解密操作不能作为web工程的某个模块,因为spring的应用是先要加载配置的.所以这个解密步骤必须是独立.而且先进行的操作.

SQLSERVER使用密码加密备份文件以防止未经授权还原数据库

原文:SQLSERVER使用密码加密备份文件以防止未经授权还原数据库 SQLSERVER使用密码加密备份文件以防止未经授权还原数据库 在备份数据库的时候,用户可以为媒体集.备份集或两者指定密码 在backup语句中,定义备份集密码和媒体密码为可选功能.使用密码可防止利用SQLSERVER工具未经授权地执行还原操作和在媒体中添加备份集. 如果指定了密码则用户还必须提供媒体密码才能执行这些操作 关于媒体集和备份集大家可以参考MSDN:http://msdn.microsoft.com/zh-cn/l