Java 實現(xiàn)加密數(shù)據(jù)庫連接的步驟
作者:月光中的污點
一、前言
在很多項目中,數(shù)據(jù)庫相關的配置文件內(nèi)容都是以明文的形式展示的,這存在一定的安全隱患。
在開發(fā)和維護項目時,不僅要關注項目的性能,同時也要注重其安全性。
二、實現(xiàn)思路
我們都知道項目啟動時,Spring 容器會加載配置文件并讀取文件中的內(nèi)容,那么我們可以下邊步驟操作:
通過 DES 算法加密連接數(shù)據(jù)庫的賬號和密碼并將加密后的密文寫到 db 配置文件中。
在 Spring 讀取 db 配置文件時將密文解密回明文。
三、實現(xiàn)編碼
3.1 加密工具類
DESUtil 類:
public 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);
}
}
}
通過上邊的工具類對連接數(shù)據(jù)庫的賬號密碼進行加密。筆者主機上連接數(shù)據(jù)庫的賬號和密碼分別是 “root” 和 “tiger”。
經(jīng)過加密后得到 “WnplV/ietfQ=” 和 “xyHEykQVHqA=” 。
db.properties 配置文件完整內(nèi)容如下:
jdbc.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 配置文件解析類
EncryptPropertyPlaceholderConfigurer 類:
public 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)容:
<!-- <context:property-placeholder location="classpath:*.properties"/> --> <bean class="com.light.ac.common.configuration.EncryptPropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:db.properties</value> </list> </property> <property name="fileEncoding" value="UTF-8"/> </bean>
未加密明文前,使用的是 <context:property-placeholder /> 加載 db 配置文件。
加密明文后,使用配置文件解析類加載 db 配置文件。
完成上述 3 個步驟后按照往常操作,直接運行項目即可。
四、總結
起初,在不了解實現(xiàn)思路前覺得這功能很神秘和高大尚。但是,理清思路后功能實現(xiàn)起來就非常簡單了。
作為程序員不能被神秘的表象驚嘆而“望而卻步”,需要學會思考和理清思路,這樣才能不斷提升自身能力。
以上就是Java 實現(xiàn)加密數(shù)據(jù)庫連接的步驟的詳細內(nèi)容,更多關于Java 實現(xiàn)加密數(shù)據(jù)庫連接的資料請關注腳本之家其它相關文章!
相關文章
解決Java調(diào)用BAT批處理不彈出cmd窗口的方法分析
本篇文章是對Java調(diào)用BAT批處理不彈出cmd窗口的方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05
Java中的System.getenv()和System.getProperty()使用詳解
文章介紹了Java中用于讀取環(huán)境配置信息的兩種方法:System.getenv()和System.getProperty(),前者讀取系統(tǒng)環(huán)境變量,返回一個不可修改的Map;后者獲取JVM環(huán)境變量值,可以通過-D參數(shù)設置,文章還提到,通過這兩種方法可以簡化配置,不需要修改代碼2024-11-11
java中超過long范圍的超大整數(shù)相加算法詳解(面試高頻)
這篇文章主要介紹了java中超過long范圍的超大整數(shù)相加算法(面試高頻),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08
解決springcloud阿里云OSS文件訪問跨域問題的實現(xiàn)
本文主要介紹了解決springcloud阿里云OSS文件訪問跨域問題的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06
java實現(xiàn)基于TCP協(xié)議網(wǎng)絡socket編程(C/S通信)
這篇文章主要介紹了java實現(xiàn)基于TCP協(xié)議網(wǎng)絡socket編程(C/S通信),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-10-10
Spring項目使用Maven和BCrypt實現(xiàn)修改密碼功能方式
在數(shù)字時代,信息安全尤其是密碼安全至關重要,本文通過實例探討如何在Spring項目中利用Maven和BCrypt實現(xiàn)一個安全的密碼修改功能,我們將從環(huán)境搭建到編寫核心業(yè)務邏輯,再到完成功能測試,確保每一步都遵循最佳安全實踐,通過本文,你將了解到密碼安全的重要性2024-10-10

