Spring数据源及配置文件数据加密实现过程详解

网友投稿 414 2022-12-07


Spring数据源及配置文件数据加密实现过程详解

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配置文件内容

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:util="http://springframework.org/schema/util"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/util https://springframework.org/schema/util/spring-util.xsd

http://springframework.org/schema/aop https://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx https://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

p:location="classpath:application.properties"

p:fileEncoding="utf-8"/>

destroy-method="close"

p:driverClassName="${driverClassName}"

p:url="${url}"

p:username="${userName}"

p:password="${password}"/>

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:util="http://springframework.org/schema/util"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/util https://springframework.org/schema/util/spring-util.xsd

http://springframework.org/schema/aop https://springframework.org/schema/aop/spring-aop.xsd

http://springframework.org/schema/tx https://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/context https://springframework.org/schema/context/spring-context.xsd">

p:location="classpath:application.properties"

p:fileEncoding="utf-8"/>

destroy-method="close"

p:driverClassName="${driverClassName}"

p:url="${url}"

p:username="${userName}"

p:password="${password}"/>

p:location="classpath:application.properties"

p:fileEncoding="utf-8"/>

destroy-method="close"

p:driverClassName="${driverClassName}"

p:url="${url}"

p:username="${userName}"

p:password="${password}"/>

destroy-method="close"

p:driverClassName="${driverClassName}"

p:url="${url}"

p:username="${userName}"

p:password="${password}"/>

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

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=


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JVM运行时数据区划分原理详解
下一篇:Java编译和解释执行对比及原理解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~