java连加密的mysql_Java 实现加密数据库连接
一、前言
在很多項(xiàng)目中,數(shù)據(jù)庫(kù)相關(guān)的配置文件內(nèi)容都是以明文的形式展示的,這存在一定的安全隱患。
在開(kāi)發(fā)和維護(hù)項(xiàng)目時(shí),不僅要關(guān)注項(xiàng)目的性能,同時(shí)也要注重其安全性。
二、實(shí)現(xiàn)思路
我們都知道項(xiàng)目啟動(dòng)時(shí),Spring 容器會(huì)加載配置文件并讀取文件中的內(nèi)容,那么我們可以下邊步驟操作:
通過(guò) DES 算法加密連接數(shù)據(jù)庫(kù)的賬號(hào)和密碼并將加密后的密文寫(xiě)到 db 配置文件中。
在 Spring 讀取 db 配置文件時(shí)將密文解密回明文。
三、實(shí)現(xiàn)編碼
3.1 加密工具類(lèi)
DESUtil 類(lèi):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57public class DESUtil {
private static Key key;
private static String KEY_STR = "myKey";
private static String CHARSETNAME = "UTF-8";
private static String ALGORITHM = "DES";
static {
try {
KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(KEY_STR.getBytes());
generator.init(secureRandom);
key = generator.generateKey();
generator = null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 加密
* @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) {
// TODO: handle exception
throw new RuntimeException(e);
}
}
/**
* 解密
* @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);
}
}
}
通過(guò)上邊的工具類(lèi)對(duì)連接數(shù)據(jù)庫(kù)的賬號(hào)密碼進(jìn)行加密。筆者主機(jī)上連接數(shù)據(jù)庫(kù)的賬號(hào)和密碼分別是 “root” 和 “tiger”。
經(jīng)過(guò)加密后得到 “WnplV/ietfQ=” 和 “xyHEykQVHqA=” 。
db.properties 配置文件完整內(nèi)容如下:
1
2
3
4jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=UTC
jdbc.username=WnplV/ietfQ=
jdbc.password=xyHEykQVHqA=
3.2 配置文件解析類(lèi)
EncryptPropertyPlaceholderConfigurer 類(lèi):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
// 需要解密的字段
private String[] encryptPropNames = { "jdbc.username", "jdbc.password" };
@Override
protected String convertProperty(String propertyName, String propertyValue) {
if (isEncryptProp(propertyName)) {
// 解密
String decryptValue = DESUtil.getDecryptString(propertyValue);
return decryptValue;
} else {
return propertyValue;
}
}
private boolean isEncryptProp(String propertyName) {
for (String encryptpropertyName : encryptPropNames) {
if (encryptpropertyName.equals(propertyName))
return true;
}
return false;
}
}
3.3 Spring 配置文件
applicationContext-mybatis.xml 部分內(nèi)容:
1
2
3
4
5
6
7
8
9
10
classpath:db.properties
未加密明文前,使用的是 加載 db 配置文件。
加密明文后,使用配置文件解析類(lèi)加載 db 配置文件。
完成上述 3 個(gè)步驟后按照往常操作,直接運(yùn)行項(xiàng)目即可。
四、總結(jié)
起初,在不了解實(shí)現(xiàn)思路前覺(jué)得這功能很神秘和高大尚。但是,理清思路后功能實(shí)現(xiàn)起來(lái)就非常簡(jiǎn)單了。
作為程序員不能被神秘的表象驚嘆而“望而卻步”,需要學(xué)會(huì)思考和理清思路,這樣才能不斷提升自身能力。
總結(jié)
以上是生活随笔為你收集整理的java连加密的mysql_Java 实现加密数据库连接的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 车联网概念股 相关公司可能迎来爆发
- 下一篇: 信用卡代还软件靠谱吗 警惕使用别被坑了