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

本文涉及的产品
密钥管理服务KMS,1000个密钥,100个凭据,1个月
简介: 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;
 }
 }

}


目录
相关文章
|
1月前
|
存储 关系型数据库 MySQL
MySQL——数据库备份上传到阿里云OSS存储
MySQL——数据库备份上传到阿里云OSS存储
79 0
|
2月前
|
Java Spring
Spring boot 运行服务jar外配置配置文件方式总结
Spring boot 运行服务jar外配置配置文件方式总结
401 0
|
1月前
|
存储 NoSQL 数据库
认证服务---整合短信验证码,用户注册和登录 ,密码采用MD5加密存储 【二】
这篇文章讲述了在分布式微服务系统中添加用户注册和登录功能的过程,重点介绍了用户注册时通过远程服务调用第三方服务获取短信验证码、使用Redis进行验证码校验、对密码进行MD5加密后存储到数据库,以及用户登录时的远程服务调用和密码匹配校验的实现细节。
认证服务---整合短信验证码,用户注册和登录 ,密码采用MD5加密存储 【二】
|
27天前
|
存储 缓存 NoSQL
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
【Azure Redis 缓存】关于Azure Cache for Redis 服务在传输和存储键值对(Key/Value)的加密问题
|
13天前
|
存储 NoSQL 关系型数据库
可以存储文件的数据库有哪些?
可以存储文件的数据库有哪些?
17 6
|
1月前
|
SQL 数据库
Spring5入门到实战------13、使用JdbcTemplate操作数据库(批量增删改)。具体代码+讲解 【下篇】
这篇文章是Spring5框架的实战教程,深入讲解了如何使用JdbcTemplate进行数据库的批量操作,包括批量添加、批量修改和批量删除的具体代码实现和测试过程,并通过完整的项目案例展示了如何在实际开发中应用这些技术。
Spring5入门到实战------13、使用JdbcTemplate操作数据库(批量增删改)。具体代码+讲解 【下篇】
|
28天前
|
存储 缓存 负载均衡
带你认识DM 共享存储数据库集群
带你认识DM 共享存储数据库集群
36 3
|
9天前
|
存储 NoSQL 关系型数据库
可以存储文件的数据库有哪些?
可以存储文件的数据库有哪些?
63 0
|
1月前
|
XML 数据库 数据格式
Spring5入门到实战------14、完全注解开发形式 ----JdbcTemplate操作数据库(增删改查、批量增删改)。具体代码+讲解 【终结篇】
这篇文章是Spring5框架的实战教程的终结篇,介绍了如何使用注解而非XML配置文件来实现JdbcTemplate的数据库操作,包括增删改查和批量操作,通过创建配置类来注入数据库连接池和JdbcTemplate对象,并展示了完全注解开发形式的项目结构和代码实现。
Spring5入门到实战------14、完全注解开发形式 ----JdbcTemplate操作数据库(增删改查、批量增删改)。具体代码+讲解 【终结篇】
|
1月前
|
SQL XML Java
Spring5入门到实战------12、使用JdbcTemplate操作数据库(增删改查)。具体代码+讲解 【上篇】
这篇文章是Spring5框架的实战教程,详细讲解了如何使用JdbcTemplate进行数据库的增删改查操作,包括在项目中引入依赖、配置数据库连接池、创建实体类、定义DAO接口及其实现,并提供了具体的代码示例和测试结果,最后还提供了完整的XML配置文件和测试代码。
Spring5入门到实战------12、使用JdbcTemplate操作数据库(增删改查)。具体代码+讲解 【上篇】