Spring數(shù)據(jù)源及配置文件數(shù)據(jù)加密實(shí)現(xiàn)過(guò)程詳解
The following example shows the corresponding XML configuration:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <context:property-placeholder location="jdbc.properties"/>
Spring在第三方依賴包中包含了兩個(gè)數(shù)據(jù)源的實(shí)現(xiàn)類包,其一是: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:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <context:property-placeholder location="jdbc.properties"/>
The following example shows C3P0 configuration:
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClassName}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <context:property-placeholder location="jdbc.properties"/>
在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ù)訪問權(quán)限的泄露.
這就要求對(duì)應(yīng)用程序配置文件對(duì)某些屬性進(jìn)行加密,讓Spring容器在讀取屬性文件后,在內(nèi)存中對(duì)屬性進(jìn)行解密,然后再將解密后的屬性賦給目標(biāo)對(duì)象.
這里提供一個(gè)加密解密工具(DES對(duì)稱加密解密)代碼:
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"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <bean class="com.springboot.utils.EncryptPropertyPlaceholderConfigurer" p:location="classpath:application.properties" p:fileEncoding="utf-8"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${driverClassName}" p:url="${url}" p:username="${userName}" p:password="${password}"/> </beans>
通過(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=
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java中在時(shí)間戳計(jì)算的過(guò)程中遇到的數(shù)據(jù)溢出問題解決
這篇文章主要介紹了Java中在時(shí)間戳計(jì)算的過(guò)程中遇到的數(shù)據(jù)溢出問題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Java IO流學(xué)習(xí)總結(jié)之文件傳輸基礎(chǔ)
這篇文章主要介紹了Java IO流學(xué)習(xí)總結(jié)之文件傳輸基礎(chǔ),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java io流的小伙伴們有很好的幫助,需要的朋友可以參考下2021-04-04JAVA WEB中Servlet和Servlet容器的區(qū)別
這篇文章主要介紹了JAVA WEB中Servlet和Servlet容器的區(qū)別,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06mybatis查詢返回Map<String,Object>類型的講解
這篇文章主要介紹了mybatis查詢返回Map<String,Object>類型的講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06使用Spring Boot實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的接口的過(guò)程
本文給大家分享使用Spring Boot實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)的接口的過(guò)程,包括springboot原理解析及實(shí)例代碼詳解,感興趣的朋友跟隨小編一起看看吧2021-07-07一篇文章帶你入門Springboot沙箱環(huán)境支付寶支付(附源碼)
螞蟻沙箱環(huán)境 (Beta) 是協(xié)助開發(fā)者進(jìn)行接口功能開發(fā)及主要功能聯(lián)調(diào)的輔助環(huán)境。沙箱環(huán)境模擬了開放平臺(tái)部分產(chǎn)品的主要功能和主要邏輯2021-06-06Java 數(shù)組內(nèi)置函數(shù)toArray詳解
這篇文章主要介紹了Java 數(shù)組內(nèi)置函數(shù)toArray詳解,文本詳細(xì)的講解了toArray底層的代碼和文檔,需要的朋友可以參考下2021-06-06獲取Java加載器和類完整結(jié)構(gòu)的方法分享
這篇文章主要為大家詳細(xì)介紹了獲取Java加載器和類完整結(jié)構(gòu)的方法,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2022-12-12springboot?pom文件加入監(jiān)控依賴后沒有起作用的解決
這篇文章主要介紹了springboot?pom文件加入監(jiān)控依賴后沒有起作用的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02