如何实现Spring数据源和配置文件数据加密-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
如何实现Spring数据源和配置文件数据加密

这篇文章主要为大家展示了如何实现Spring数据源和配置文件数据加密,内容简而易懂,希望大家可以学习一下,学习完之后肯定会有收获的,下面让小编带大家一起来看看吧。

创新互联公司主要从事成都网站建设、网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务封丘,十多年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务:13518219792

The following example shows the corresponding XML configuration:


  
  
  
  


Spring在第三方依赖包中包含了两个数据源的实现类包,其一是:Apache的DBCP;其二是C3P0,可以在Spring配置文件中利用二者的任何一个配置数据源.

The next two examples show the basic connectivity and configuration for DBCP and C3P0. To learn about more options that help control the pooling features, see the product documentation for the respective connection pooling implementations.

The following example shows DBCP configuration:


  
  
  
  


The following example shows C3P0 configuration:


  
  
  
  


在jdbc.properties文件中定义属性的值,如下:

jdbc.driverClassName=com.MySQL.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3309/sampledb

jdbc.username=root

jdbc.password=123456

但是这些属性是以明文形式存放,那么任何拥有服务器登录权限的人都可以查看这些机密信息,容易造成数据库访问权限的泄露.

这就要求对应用程序配置文件对某些属性进行加密,让Spring容器在读取属性文件后,在内存中对属性进行解密,然后再将解密后的属性赋给目标对象.

这里提供一个加密解密工具(DES对称加密解密)代码:

package com.springboot.utils;

import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;


public class DESUtils {
  //指定DES加密解密所用的密钥
  private static Key key;
  private static String KEY_STR = "myKey";
  
  static {
    try {
      KeyGenerator generator = KeyGenerator.getInstance("DES");
      generator.init(new SecureRandom(KEY_STR.getBytes()));
      key = generator.generateKey();
      generator = null;
    }catch(Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  public static String getEncryptString(String str) {
    Encoder encoder = Base64.getEncoder();
    try {
      byte[] strBytes = str.getBytes("UTF8");
      Cipher cipher = Cipher.getInstance("DES");
      cipher.init(Cipher.ENCRYPT_MODE, key);
      byte[] encryptStrBytes = cipher.doFinal(strBytes);
      return encoder.encodeToString(encryptStrBytes);
    }catch(Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  public static String getDecryptString(String str) {
    Decoder decoder = Base64.getDecoder();
    try {
      byte[] strBytes = decoder.decode(str);
      Cipher cipher = Cipher.getInstance("DES");
      cipher.init(Cipher.DECRYPT_MODE, key);
      byte[] decryptStrBytes = cipher.doFinal(strBytes);
      return new String(decryptStrBytes,"UTF8");
    }catch(Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  public static void main(String[] args) throws Exception{
    if(args == null || args.length < 1) {
      System.out.println("请输入要加密的字符,用空格分隔.");
    }else {
      for(String arg : args) {
        System.out.println(arg + ":" + getEncryptString(arg));
      }
    }
  }
}

针对配置文件中加密信息的解密

package com.springboot.utils;

import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

public class EncryptPropertyPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer{
  private String[] encryptPropNames = {"userName","password"};
  
  private boolean isEncryptProp(String propertyName) {
    for(String encryptProName : encryptPropNames) {
      if(encryptProName.equals(propertyName)) {
        return true;
      }
    }
    return false;
  }
  @Override
  protected String convertProperty(String propertyName, String propertyValue) {
    if(isEncryptProp(propertyName)) {
      String decryptVal = DESUtils.getDecryptString(propertyValue);
      System.out.println("decryptVal = " + decryptVal);
      return decryptVal;
    }else {
      return propertyValue;
    }
  }
}

xml配置文件内容

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



   
   
   

通过在控制台运行我们的加密代码获取加密后的密文

yusuwudeMacBook-Pro:classes yusuwu$ java com.springboot.utils.DESUtils root 123

获取密文:

root:jxlNoW/DjKw=

123:RbtzyNE4tjY=

在application.properties中配置

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/springboot
userName=jxlNoW/DjKw=
password=RbtzyNE4tjY=

以上就是关于如何实现Spring数据源和配置文件数据加密的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。


网页标题:如何实现Spring数据源和配置文件数据加密
网址分享:http://scgulin.cn/article/iiohsg.html