国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Java 實(shí)現(xiàn)加密數(shù)據(jù)庫連接的步驟

瀏覽:83日期:2022-08-21 13:29:57

作者:月光中的污點(diǎn)

原文鏈接:https://www.extlight.com/2017/12/16/Java-%E5%AE%9E%E7%8E%B0%E5%8A%A0%E5%AF%86%E6%95%B0%E6%8D%AE%E5%BA%93%E8%BF%9E%E6%8E%A5/

一、前言

在很多項(xiàng)目中,數(shù)據(jù)庫相關(guān)的配置文件內(nèi)容都是以明文的形式展示的,這存在一定的安全隱患。

在開發(fā)和維護(hù)項(xiàng)目時,不僅要關(guān)注項(xiàng)目的性能,同時也要注重其安全性。

二、實(shí)現(xiàn)思路

我們都知道項(xiàng)目啟動時,Spring 容器會加載配置文件并讀取文件中的內(nèi)容,那么我們可以下邊步驟操作:

通過 DES 算法加密連接數(shù)據(jù)庫的賬號和密碼并將加密后的密文寫到 db 配置文件中。

在 Spring 讀取 db 配置文件時將密文解密回明文。

三、實(shí)現(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ù)庫的賬號密碼進(jìn)行加密。筆者主機(jī)上連接數(shù)據(jù)庫的賬號和密碼分別是 “root” 和 “tiger”。

經(jīng)過加密后得到 “WnplV/ietfQ=” 和 “xyHEykQVHqA=” 。

db.properties 配置文件完整內(nèi)容如下:

jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=UTCjdbc.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 個步驟后按照往常操作,直接運(yùn)行項(xiàng)目即可。

四、總結(jié)

起初,在不了解實(shí)現(xiàn)思路前覺得這功能很神秘和高大尚。但是,理清思路后功能實(shí)現(xiàn)起來就非常簡單了。

作為程序員不能被神秘的表象驚嘆而“望而卻步”,需要學(xué)會思考和理清思路,這樣才能不斷提升自身能力。

以上就是Java 實(shí)現(xiàn)加密數(shù)據(jù)庫連接的步驟的詳細(xì)內(nèi)容,更多關(guān)于Java 實(shí)現(xiàn)加密數(shù)據(jù)庫連接的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 免费看又黄又爽又猛的网站 | 国产精品美女一级在线观看 | 亚洲精品一区二区观看 | 亚洲精品区在线播放一区二区 | 国产a一级 | 91视频一88av| 色拍拍在精品视频69影院在线 | 国产一区二区日韩欧美在线 | 久久久久国产精品免费看 | 美女视频免费黄 | 成年女人午夜免费视频 | 婷婷尹人香蕉久久天堂 | 国产精品所毛片视频 | 免费看裸色 | 亚洲美女在线观看亚洲美女 | 国产精品系列在线 | 波多野结衣中文一区二区免费 | 国产午夜免费福利红片 | 午夜视频国产 | 一本不卡 | 中文日韩字幕一区在线观看 | 日本免费特黄aa毛片 | 国产成年人在线观看 | 九九视频在线观看 | 亚欧视频在线 | 成人黄色一级视频 | 精品videosex性欧美 | 另类专区欧美 | 国产三区二区 | 午夜一区二区福利视频在线 | 高清精品一区二区三区一区 | 久久精品国产亚洲网站 | 欧美日韩一区二区在线观看视频 | 国产成人综合精品 | 成人久久网 | 思思久热re6这里有精品 | 欧美性猛交xxxx免费看手交 | 欧美性久久久久 | 美女黄色一级毛片 | 亚洲男人的天堂久久精品 | 亚洲天堂成人在线观看 |