這篇文章主要為大家展示了如何實(shí)現(xiàn)Spring數(shù)據(jù)源和配置文件數(shù)據(jù)加密,內(nèi)容簡(jiǎn)而易懂,希望大家可以學(xué)習(xí)一下,學(xué)習(xí)完之后肯定會(huì)有收獲的,下面讓小編帶大家一起來(lái)看看吧。
創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)封丘,十多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專(zhuān)業(yè),歡迎來(lái)電咨詢建站服務(wù):13518219792
The following example shows the corresponding XML configuration:
Spring在第三方依賴(lài)包中包含了兩個(gè)數(shù)據(jù)源的實(shí)現(xiàn)類(lèi)包,其一是:Apache的DBCP;其二是C3P0,可以在Spring配置文件中利用二者的任何一個(gè)配置數(shù)據(jù)源.
The next two examples show the basic connectivity and configuration for DBCP and C3P0. To learn about more options that help control the pooling features, see the product documentation for the respective connection pooling implementations.
The following example shows DBCP configuration:
The following example shows C3P0 configuration:
在jdbc.properties文件中定義屬性的值,如下:
jdbc.driverClassName=com.MySQL.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3309/sampledb
jdbc.username=root
jdbc.password=123456
但是這些屬性是以明文形式存放,那么任何擁有服務(wù)器登錄權(quán)限的人都可以查看這些機(jī)密信息,容易造成數(shù)據(jù)庫(kù)訪問(wèn)權(quán)限的泄露.
這就要求對(duì)應(yīng)用程序配置文件對(duì)某些屬性進(jìn)行加密,讓Spring容器在讀取屬性文件后,在內(nèi)存中對(duì)屬性進(jìn)行解密,然后再將解密后的屬性賦給目標(biāo)對(duì)象.
這里提供一個(gè)加密解密工具(DES對(duì)稱(chēng)加密解密)代碼:
package com.springboot.utils; import java.security.Key; import java.security.SecureRandom; import java.util.Base64; import java.util.Base64.Decoder; import java.util.Base64.Encoder; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; public class DESUtils { //指定DES加密解密所用的密鑰 private static Key key; private static String KEY_STR = "myKey"; static { try { KeyGenerator generator = KeyGenerator.getInstance("DES"); generator.init(new SecureRandom(KEY_STR.getBytes())); key = generator.generateKey(); generator = null; }catch(Exception e) { throw new RuntimeException(e); } } public static String getEncryptString(String str) { Encoder encoder = Base64.getEncoder(); try { byte[] strBytes = str.getBytes("UTF8"); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptStrBytes = cipher.doFinal(strBytes); return encoder.encodeToString(encryptStrBytes); }catch(Exception e) { throw new RuntimeException(e); } } public static String getDecryptString(String str) { Decoder decoder = Base64.getDecoder(); try { byte[] strBytes = decoder.decode(str); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptStrBytes = cipher.doFinal(strBytes); return new String(decryptStrBytes,"UTF8"); }catch(Exception e) { throw new RuntimeException(e); } } public static void main(String[] args) throws Exception{ if(args == null || args.length < 1) { System.out.println("請(qǐng)輸入要加密的字符,用空格分隔."); }else { for(String arg : args) { System.out.println(arg + ":" + getEncryptString(arg)); } } } }
針對(duì)配置文件中加密信息的解密
package com.springboot.utils; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; public class EncryptPropertyPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer{ private String[] encryptPropNames = {"userName","password"}; private boolean isEncryptProp(String propertyName) { for(String encryptProName : encryptPropNames) { if(encryptProName.equals(propertyName)) { return true; } } return false; } @Override protected String convertProperty(String propertyName, String propertyValue) { if(isEncryptProp(propertyName)) { String decryptVal = DESUtils.getDecryptString(propertyValue); System.out.println("decryptVal = " + decryptVal); return decryptVal; }else { return propertyValue; } } }
xml配置文件內(nèi)容
<?xml version="1.0" encoding="UTF-8"?>
通過(guò)在控制臺(tái)運(yùn)行我們的加密代碼獲取加密后的密文
yusuwudeMacBook-Pro:classes yusuwu$ java com.springboot.utils.DESUtils root 123
獲取密文:
root:jxlNoW/DjKw=
123:RbtzyNE4tjY=
在application.properties中配置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/springboot
userName=jxlNoW/DjKw=
password=RbtzyNE4tjY=
以上就是關(guān)于如何實(shí)現(xiàn)Spring數(shù)據(jù)源和配置文件數(shù)據(jù)加密的內(nèi)容,如果你們有學(xué)習(xí)到知識(shí)或者技能,可以把它分享出去讓更多的人看到。