Spring配置文件中數(shù)據(jù)庫配置文件properties文件的加密
思路:在項(xiàng)目開始運(yùn)行之前,先使用加密程序把正確的明文密碼進(jìn)行加密得到密文信息,修改配置文件中的password為密文,在項(xiàng)目啟動的時候要把這個密文進(jìn)行解開為明文、讓spring連接數(shù)據(jù)庫使用。
參考文章來源
一、加密原因
數(shù)據(jù)庫連接進(jìn)行加密的主要目的是為了保證應(yīng)用軟件的數(shù)據(jù)的正確性,防止惡意修改。
在Spring配置文件中,將數(shù)據(jù)庫配置信息存儲在properties文件中可以方便地管理和維護(hù)應(yīng)用程序的配置。然而,如果這些敏感信息(例如數(shù)據(jù)庫用戶名和密碼)未加密,在不法分子的攻擊下,被獲取和濫用進(jìn)行惡意攻擊和數(shù)據(jù)泄漏的風(fēng)險將會顯著增加。文章來源:http://www.zghlxwxcb.cn/news/detail-484104.html
#這里db.properties文件中外露了數(shù)據(jù)庫連接信息,這會導(dǎo)致數(shù)據(jù)庫存在很大的安全隱患
db.username=root
db.password=123456
db.url=jdbc:mysql:///schoolproject?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
db.driverClass=com.mysql.cj.jdbc.Driver
二、對SSM項(xiàng)目中數(shù)據(jù)庫連接加密流程
- 首先,使用一個加密工具類實(shí)現(xiàn)對數(shù)據(jù)庫連接的url、username、password、driverClass等明文信息通過工具類加密成密文。
package com.yjr.utils;
public class KaisaUtil {
/***
* 使用凱撒加密方式加密數(shù)據(jù)
* @param orignal 原文
* @param key 密鑰
* @return 加密后的字符
*/
public static String encryptKaisa(String orignal, int key) {
//將字符串轉(zhuǎn)換為數(shù)組
char[] chars = orignal.toCharArray();
StringBuffer buffer = new StringBuffer();
//遍歷數(shù)組
for(char aChar : chars) {
//獲取字符的ASCII編碼
int asciiCode = aChar;
//偏移數(shù)據(jù)
asciiCode += key;
//將偏移后的數(shù)據(jù)轉(zhuǎn)為字符
char result = (char)asciiCode;
//拼接數(shù)據(jù)
buffer.append(result);
}
return buffer.toString();
}
/**
* 使用凱撒加密方式解密數(shù)據(jù)
*
* @param encryptedData :密文
* @param key :密鑰
* @return : 源數(shù)據(jù)
*/
public static String decryptKaiser(String encryptedData, int key) {
// 將字符串轉(zhuǎn)為字符數(shù)組
char[] chars = encryptedData.toCharArray();
StringBuilder sb = new StringBuilder();
// 遍歷數(shù)組
for (char aChar : chars) {
// 獲取字符的ASCII編碼
int asciiCode = aChar;
// 偏移數(shù)據(jù)
asciiCode -= key;
// 將偏移后的數(shù)據(jù)轉(zhuǎn)為字符
char result = (char) asciiCode;
// 拼接數(shù)據(jù)
sb.append(result);
}
return sb.toString();
}
public static void main(String[] args) {
String str = "com.mysql.cj.jdbc.Driver";
String encode = encryptKaisa(str,255);
System.out.println("加密后:"+encode);
System.out.println(encode);
String decode = decryptKaiser(encode, 255);
System.out.println("解密后:"+decode);
}
- 其次:將獲取到密文數(shù)據(jù)后將db.properties中的明文改成密文
#數(shù)據(jù)庫連接密文--此時數(shù)據(jù)庫中的數(shù)據(jù)較為安全
db.username=????
db.password=UVWXYZ
db.url=?????????ū?????????ū?????????????????????????????????ń??????????????????????????????????????????
db.driverClass=????????ū???????????????
- 自定義PropertiesPersist 解密類
該類繼承spring的讀取配置文件的類使spring啟動時會加載這個類進(jìn)行解密(需要在配置文件里面配置這個類的名稱才會進(jìn)行加載)
本項(xiàng)目是在applicationContext.xml里面配置了文章來源地址http://www.zghlxwxcb.cn/news/detail-484104.html
package com.yjr.config;
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 com.yjr.utils.KaisaUtil;
import org.springframework.util.DefaultPropertiesPersister;
//該類繼承spring的讀取配置文件的類使spring啟動時會加載這個類進(jìn)行解密(需要在配置文件里面配置這個類的名稱才會進(jìn)行加載)
//本項(xiàng)目是在applicationContext.xml里面配置了
//項(xiàng)目啟動時候可以在這個類中打斷點(diǎn)進(jìn)入看解密過程
public class PropertiesPersist extends DefaultPropertiesPersister{
@Override
public void load(Properties props, InputStream is) throws IOException {
//讀取到配置文件(****..properties)信息內(nèi)容
Properties properties=new Properties();
properties.load(is);
//判斷讀取配置文件中的信息是否為password屬性
if(properties.get("db.password")!=null){
System.out.println("==========密文======="+properties.get("db.password"));
System.out.println("==========密文======="+properties.get("db.username"));
System.out.println("==========密文======="+properties.get("db.url"));
System.out.println("==========密文======="+properties.get("db.driverClass"));
System.out.println("===================進(jìn)行配置數(shù)據(jù)庫密碼解密、注入配置文件===");
//進(jìn)行解密
String username= KaisaUtil.decryptKaiser(properties.get("db.username").toString().trim(),255);
String driverClass= KaisaUtil.decryptKaiser(properties.get("db.driverClass").toString().trim(),255);
String url= KaisaUtil.decryptKaiser(properties.get("db.url").toString().trim(),255);
String password= KaisaUtil.decryptKaiser(properties.get("db.password").toString().trim(),36);
System.out.println("解密后的password="+password);
System.out.println("解密后的username="+username);
System.out.println("解密后的url="+url);
System.out.println("解密后的driverClass="+driverClass);
//在寫入到配置文件中
properties.setProperty("db.password", password);
properties.setProperty("db.username", username);
properties.setProperty("db.driverClass", driverClass);
properties.setProperty("db.url", url);
}
OutputStream outputStream=null;
try {
outputStream=new ByteArrayOutputStream();
properties.store(outputStream, "");
is=outStream2InputStream(outputStream);
super.load(props, is);
System.out.println("=========配置數(shù)據(jù)庫密碼解密、注入配置文件=========【Success】==");
System.out.println("=======注入的明文密碼======:" +properties.get("db.password"));
System.out.println("=======注入的明文賬號======:" +properties.get("db.username"));
System.out.println("=======注入的明文url======:" +properties.get("db.url"));
System.out.println("=======注入的明文driverClass=====:" +properties.get("db.driverClass"));
} 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;
}
}
- 最后:在applicationContext.xml修改配置文件
<!-- 首先:將原本的<context:property-placeholder location="classpath:db.properties"/> 修改為新的bean -->
<!-- 它定義了一個名為 configReader 的 Bean 對象,用于讀取數(shù)據(jù)庫配置文件 db.properties-->
<bean id="configReader" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<!-- class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer":指定了這個 Bean 對象實(shí)現(xiàn)的類。-->
<property name="locations">
<list>
<value>classpath*:db.properties</value>
</list>
</property>
<!-- propertiesPersister 屬性:表示將要使用一個自定義的 PropertiesPersist 類對象實(shí)現(xiàn)對配置文件中屬性的解密操作。-->
<property name="propertiesPersister">
<!-- 自定義解密類的全稱-->
<bean class="com.yjr.config.PropertiesPersist"/>
</property>
<!-- ignoreResourceNotFound 屬性:設(shè)為 true; 表示即使沒有找到這個配置文件也不會拋出異常。-->
<property name="ignoreResourceNotFound" value="true"/>
</bean>
到了這里,關(guān)于SSM項(xiàng)目中數(shù)據(jù)庫連接properties文件加密的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!