传统JAVA_WEB项目如果是ssh、ssm之类的搭建的项目,其数据库链接信息大部分是交给Spring来作为管理
数据库的URL地址、账号、密码一般会写在配置文件中:dbconfig.properties
里或者是在system-config.properties
在或者是在applicationContext-datasource.xml
配置,无非就是这几种。在或者是你的数据库链接信息在项目的那个自己命名的配置文件里面记录。
为了做到保护数据安全、版权原因等一些效果。提出需求要对项目中所有涉及数据库链接信息地方的账号密码进行加密
思路:在项目开始运行之前,先使用加密程序把正确的明文密码进行加密得到密文信息,修改配置文件中的password为密文,在项目启动的时候要把这个密文进行解开为明文、让spring链接数据库使用
- 加密解密程序(使用开源MD5加密解密)可以修改加密程序,加上自己的独特的字符串数字之类的逻辑、就是自己独特的加密程序了。
import java.security.MessageDigest;
/**
* MD5技术加密解密
*/
public class MD5Tools {
/***
* MD5加码 生成32位md5码
*/
public static String string2MD5(String inStr){
MessageDigest md5 = null;
try{
md5 = MessageDigest.getInstance("MD5");
}catch (Exception e){
System.out.println(e.toString());
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++){
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
/**
* 加密解密算法 执行一次加密,两次解密
*/
public static String convertMD5(String inStr){
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++){
a[i] = (char) (a[i] ^ 't');
}
String s = new String(a);
return s;
}
// 测试主函数
public static void main(String args[]) {
String s = new String("rootroot");
System.out.println("原始:" + s);
System.out.println("MD5后:" + string2MD5(s));
System.out.println("解密的:" + convertMD5(convertMD5(s)));
}
}
2.数据库的链接信息、如果配置是在****.xml里面需要移动链接信息配置到****…properties中。如果项目中没有****…properties就自己新建一个、然后*******.xml中使用占位符。如果项目的数据库链接信息已经在****…properties就不用再修改了(移动的目的是读取配置文件信息时候,****…properties比********.xml读取方便)
3.在项目启动时候让spring加载自定义的类,进行密码解密。按照图中配置信息进行配置,注意自定义类的名称和路径。
package com.xxx.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import org.springframework.util.DefaultPropertiesPersister;
//该类继承spring的读取配置文件的类使spring启动时会加载这个类进行解密(需要在配置文件里面配置这个类的名称才会进行加载)
//本项目是在applicationContext-datasource.xml里面配置了
//项目启动时候可以在这个类中打断点进入看解密过程
public class PropertiesPersist extends DefaultPropertiesPersister{
@Override
public void load(Properties props, InputStream is) throws IOException {
//读取到配置文件(****..properties)信息内容
Properties properties=new Properties();
properties.load(is);
//判断读取配置文件中的信息是否为password属性
if(properties.get("password")!=null){
System.out.println("==========密文======="+properties.get("password"));
System.out.println("===================进行配置数据库密码解密、注入配置文件===");
//进行解密
String password=MD5Tools.convertMD5(properties.get("password").toString().trim());
//在写入到配置文件中
properties.setProperty("password", password);
}
OutputStream outputStream=null;
try {
outputStream=new ByteArrayOutputStream();
properties.store(outputStream, "");
is=outStream2InputStream(outputStream);
super.load(props, is);
System.out.println("=========配置数据库密码解密、注入配置文件=========【Success】==");
System.out.println("=======注入的明文密码======:" +properties.get("password"));
} catch (Exception e) {
try {
throw e;
} catch (Exception e1) {
e1.printStackTrace();
}
}finally{
outputStream.close();
}
}
private InputStream outStream2InputStream(OutputStream out) {
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bos=(ByteArrayOutputStream) out;
ByteArrayInputStream swapSTream=new ByteArrayInputStream(bos.toByteArray());
return swapSTream;
}
}
4.项目启动时候可以在自定义的类打断点,跟一下是否进入到解密类中,密码是否进行了解密,输出明文,验证为项目中没有错误的的日志信息,项目可以正常访问,加密解密到此文为止,账号加密可以参考密码加密。