• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

spring*.xml配置文件明文加密的实现

java 搞代码 4年前 (2022-01-09) 32次浏览 已收录 0个评论

说明:客户要求spring*.xml中Oracle/Redis/MongoDB的IP、端口、用户名、密码不能明文存放,接到需求的我,很无奈,但是还是的硬着头皮搞

系统架构:spring+mvc(Oracle是用jdbc自己封装的接口)

1.数据库配置文件加密

原xml配置

<?xml version="1.0" encoding="utf-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
 default-autowire="byType">
  
<context:component-scan base-package="cn.geoff" use-default-filters="false">
 <context:include-filter type="annotation" expression="org.springframework.stereotype.Service"/>
 </context:component-scan>

 <!-- Database Connection Pool -->
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
 <property name="url" value="jdbc:oracle:thin:@192.168.100.100:1521:orcl"/>
 <property name="username" value="Geoff"/>
 <property name="password" value="123456"/>
 <property name="validationQuery" value="select 'x' from dual"/>

 .....
 </bean>

 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource"/>
 </bean>
  
  </beans>

加密实现过程

思路:继承DruidDataSource,在初始化set值的时候进行解密

/**
 * 数据库连接解密
 * @author: Geoff
 * @create: 2020-12-30 16:46
 **/
public class DataBaseXml extends DruidDataSource {


  /**
   * Log4j logger
   */
  private final static Logger lg = LoggerFactory.getLogger(DataBaseXml.class);

  @Override
  public String getUrl() {
    return this.jdbcUrl;
  }

  @Override
  public void setUrl(String jdbcUrl) {
    if(GEOFF.DATA_BASE_IS_ENCRYPTION) {
      lg.info("数据库【jdbcUrl】解密初始化加载...");
      try {
        jdbcUrl = Encryption.decryp<a style="color:transparent">来@源gao*daima.com搞@代#码网</a>t(jdbcUrl, GEOFF.DATA_BASE_ENCRYPTION_KEY);
      } catch (Exception e) {
        lg.error("数据库【jdbcUrl】密文解密失败...");
        e.printStackTrace();
      }
    }
    this.jdbcUrl = jdbcUrl;
  }

  @Override
  public String getUsername() {
    return this.username;
  }

  @Override
  public void setUsername(String username) {
    if(GEOFF.DATA_BASE_IS_ENCRYPTION) {
      lg.info("数据库【username】解密初始化加载...");
      try {
        username = Encryption.decrypt(username, GEOFF.DATA_BASE_ENCRYPTION_KEY);
      } catch (Exception e) {
        lg.error("数据库【username】密文解密失败...");
        e.printStackTrace();
      }
    }
    this.username = username;
  }

  @Override
  public String getPassword() {
    return this.password;
  }

  @Override
  public void setPassword(String password) {
    if(GEOFF.DATA_BASE_IS_ENCRYPTION){
      lg.info("数据库【password】解密初始化加载...");
      try {
        password = Encryption.decrypt(password, GEOFF.DATA_BASE_ENCRYPTION_KEY);
      } catch (Exception e) {
        lg.error("数据库【password】密文解密失败...");
        e.printStackTrace();
      }
    }
    this.password = password;
  }



}


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:spring*.xml配置文件明文加密的实现
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址