mybatis中对数据库连接信息进行加密

2025-11-09 10:58:11

1、第一步在自己的项目中写一个简单加密解密算法类,在这里我选择的使用DES加密,类的主要内容如下:

/**

 * <描述>

 * @author <姓名>

 * @date 2014-10-8 下午3:24:14

 * DESUtils

 */

package com.cyl.util;

import java.security.Key;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

/**

 * DES加密算法工具类

 * 

 * @author RyanCai

 * @date 2014-10-8 下午3:24:14

 */

public class DESUtils {

private static Key key;

private static String KEY_STR = "myKeyRyanCai";// 密钥

private static String CHARSETNAME = "UTF-8";// 编码

private static String ALGORITHM = "DES";// 加密类型

static {

try {

KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);

generator.init(new SecureRandom(KEY_STR.getBytes()));

key = generator.generateKey();

generator = null;

} catch (Exception e) {

throw new RuntimeException(e);

}

}

/**

* 对str进行DES加密

* @param str

* @return

*/

public static String getEncryptString(String str) {

BASE64Encoder base64encoder = new BASE64Encoder();

try {

byte[] bytes = str.getBytes(CHARSETNAME);

Cipher cipher = Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.ENCRYPT_MODE, key);

byte[] doFinal = cipher.doFinal(bytes);

return base64encoder.encode(doFinal);

} catch (Exception e) {

throw new RuntimeException(e);

}

}

/**

* 对str进行DES解密

* @param str

* @return

*/

public static String getDecryptString(String str) {

BASE64Decoder base64decoder = new BASE64Decoder();

try {

byte[] bytes = base64decoder.decodeBuffer(str);

Cipher cipher = Cipher.getInstance(ALGORITHM);

cipher.init(Cipher.DECRYPT_MODE, key);

byte[] doFinal = cipher.doFinal(bytes);

return new String(doFinal, CHARSETNAME);

} catch (Exception e) {

throw new RuntimeException(e);

}

}

public static void main(String[] args) {

String cod = getEncryptString("123456");

System.out.println(cod);

}

}

2、再使用加密类将自己jdbc.properties中重要的文件进行加密,这样jdbc.properties文件中显示的就是加密信息,如下图

mybatis中对数据库连接信息进行加密

3、前面准备好了之后就需要修改mybatis中初始化SqlSessionFactory的代码,这里我只是写了一个简单的例子来说明,代码如下:

//用静态语句块来完成SqlSessionFactory的创建

static {

try {

//第一种方式

Reader reader=Resources.getResourceAsReader("mybatis.xml");

Properties ppt=Resources.getResourceAsProperties("jdbc.properties");

String username=ppt.getProperty("username");

String password=ppt.getProperty("password");

System.out.println("原密文="+username+"/"+password);

DESUtils des=new DESUtils();

System.out.println("解密后="+des.getDecryptString(username)+"/"+des.getDecryptString(password));

ppt.setProperty("username", des.getDecryptString(username));

ppt.setProperty("password", des.getDecryptString(password));

ssf = new SqlSessionFactoryBuilder().build(reader,ppt);

} catch (Exception e) {

e.printStackTrace();

}

}

4、这里需要说明的是new SqlSessionFactoryBuilder().build(reader,ppt);这个方法,mybatis中reader也会加载mybatis的配置文件,但是后面设置进来的Properties ppt这个属性类会将原配置文件中的某些属性用新值替换掉。所以还是会起到一个加密的作用。执行测试后,打印输出如下:

mybatis中对数据库连接信息进行加密

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢