一般來講,MyBatis的數據源都是通過這種形式直接配置的:
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
<property name="username" value="root" />
<property name="password" value="11111111" />
</dataSource>
</environment>
</environments>
</configuration>
顯然,這樣把數據庫連接的所有信息,明文的這樣展示出來是一件很危險的事情。
所以,我就去研究了一下,如果對數據源信息進行加密。
我採用的就是簡單Base64對文件進行加密,當然,有人說java是可以反編譯的,對,沒錯,我們只防君子,不能防小人。
所以,我要做的事情是Base64對MyBatis的數據源進行加密.
那麼,整理下思路,我要做的工作,主要有以下3點:
1.將url,username,password,進行加密,獲得密文
2.將密文配置到MyBatis中,並MyBatis的創建SqlSessionFactory之前,對密文進行解密,然後獲得數據庫連接
3.關聯數據源
1.將url,username,password,進行加密,獲得密文
/**
* 工具類,將Sting字符串進行加密
*
* @author robin
*
*/
public class String2Coder {
public static void main(String[] args) {
if (args.length < 1)
return;
byte[] buffer = Base64.getEncoder().encode(args[0].getBytes());
System.out.println(new String(buffer));
try {
System.out.println("press any key to exit.");
System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
}
這個類很簡單,就是使用Base64對字段進行加密。當然,出於方便,可以直接將args[0]換成我們需要加密的字符串。
但是,我這樣做,是爲了順便做個Base64加密的工具,然後將這個類導出爲可執行的jar包。
然後,打開cmd,執行jar包,注意最後的 >out.txt 是將輸出結果重定向到out.txt
打開out.txt文件:
cm9iaW4就是robin加密後的密文啦。
2.將密文配置到MyBatis中
2.1 首先需要說明的是,MyBatis,是支持將數據源分離成jdbc.propertis,所以第一步是準備jdbc.properties
jdbc.properties:
driver=com.mysql.jdbc.Driver url=****** username=****** password=****** |
2.2 通過代碼獲取jdbc.properties並將字段解密,創建SqlSessionFactory
package com.test.sql.session;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.Properties;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
public class SqlSessionFactoryUtil {
public static Logger logger = Logger.getLogger(SqlSessionFactoryUtil.class);
private static SqlSessionFactory factory;
public static Class<SqlSessionFactoryUtil> LOCK = SqlSessionFactoryUtil.class;
public static SqlSession getSqlSession() {
SqlSessionFactory sf = getFactory();
return getFactory().openSession();
}
private static SqlSessionFactory getFactory() {
if (factory != null)
return factory;
synchronized (LOCK) {
if (factory == null) {
InputStream inputStream = null;
try {
inputStream = Resources
.getResourceAsStream("mybatis-config.xml");
// read the encode jdbc.properties
Properties prop = getJdbcPropertiesByDecode("jdbc.properties");
factory = new SqlSessionFactoryBuilder().build(inputStream,
prop);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} finally {
if (inputStream != null)
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return factory;
}
private static Properties getJdbcPropertiesByDecode(String string)
throws IOException {
Properties prop = Resources.getResourceAsProperties("jdbc.properties");
// String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
byte[] buffer = Base64.getDecoder().decode(url.getBytes());
prop.setProperty("url", new String(buffer));
buffer = Base64.getDecoder().decode(username.getBytes());
prop.setProperty("username", new String(buffer));
buffer = Base64.getDecoder().decode(password.getBytes());
prop.setProperty("password", new String(buffer));
return prop;
}
public static void main(String[] args) throws IOException {
Properties prop = Resources.getResourceAsProperties("jdbc.properties");
// String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
byte[] buffer = Base64.getDecoder().decode(url.getBytes());
prop.setProperty("url", new String(buffer));
buffer = Base64.getDecoder().decode(username.getBytes());
prop.setProperty("username", new String(buffer));
buffer = Base64.getDecoder().decode(password.getBytes());
prop.setProperty("password", new String(buffer));
System.out.println(prop);
}
}
注意看getJdbcPropertiesByDecode方法,解密過程;
注意看factory = new SqlSessionFactoryBuilder().build(inputStream,prop);很清晰吧。
main,是用來測試我加密的密文,解密之後是否正確。
but!!!!到這裏並沒有結束。
筆者第一次配置的時候,天真的把dataSource全部刪除掉了,我猜大概這樣可以了吧,但,你會發現,並不行。
3.關聯數據源
爲什麼不行,可以思考下,雖然我們通過factory = new SqlSessionFactoryBuilder().build(inputStream,prop);中的prop把url,username,password注入了進去,但還是沒有獲取到數據源。從設計的角度來看,jdbc.properties裏面,我完全可以將url寫成urllllllll,username寫成name,password寫成pwd。這樣MyBatis是無法識別,所以最後一步就是關聯。
再來看下關聯後的mybatis-config.xml
mybatis-config.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
</configuration>
此處的$url對應的就是第二部 prop.set("url",url);中的"url"了。
到此,結束。
筆者突然想到一個問題,就是MyBatis如何配置多個數據源。
其實很簡單嘛,有幾個數據源就配置幾個mybatis-config.xml,也對應幾個SqlSessionFactory。
完全沒有任何問題。