亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Spring數據源及配置文件數據加密實現過程詳解

瀏覽:82日期:2023-09-06 14:32:21

The following example shows the corresponding XML configuration:

<bean 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在第三方依賴包中包含了兩個數據源的實現類包,其一是:Apache的DBCP;其二是C3P0,可以在Spring配置文件中利用二者的任何一個配置數據源.

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 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 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

但是這些屬性是以明文形式存放,那么任何擁有服務器登錄權限的人都可以查看這些機密信息,容易造成數據庫訪問權限的泄露.

這就要求對應用程序配置文件對某些屬性進行加密,讓Spring容器在讀取屬性文件后,在內存中對屬性進行解密,然后再將解密后的屬性賦給目標對象.

這里提供一個加密解密工具(DES對稱加密解密)代碼:

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('請輸入要加密的字符,用空格分隔.'); }else { for(String arg : args) {System.out.println(arg + ':' + getEncryptString(arg)); } } }}

針對配置文件中加密信息的解密

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配置文件內容

<?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 p:location='classpath:application.properties' p:fileEncoding='utf-8'/> <beandestroy-method='close' p:driverClassName='${driverClassName}' p:url='${url}' p:username='${userName}' p:password='${password}'/></beans>

通過在控制臺運行我們的加密代碼獲取加密后的密文

yusuwudeMacBook-Pro:classes yusuwu$ java com.springboot.utils.DESUtils root 123

獲取密文:

root:jxlNoW/DjKw=

123:RbtzyNE4tjY=

在application.properties中配置

driverClassName=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/springbootuserName=jxlNoW/DjKw=password=RbtzyNE4tjY=

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 动漫精品专区一区二区三区不卡 | 亚色一区 | 97青草最新免费精品视频 | 91麻豆视频在线 | 亚洲免费专区 | 国产精品嫩草影院在线 | 午夜一区 | 日本特交大片免费观看 | 视频在线观看一区二区三区 | 国产亚洲精品美女2020久久 | 一级黄片一级毛片 | 亚洲精品1区 | 色综合久久夜色精品国产 | 日本高清免费毛片久久看 | 中文字幕一区婷婷久久 | 成年人黄色在线观看 | 国产伦精品一区二区三区 | 精品国产女同疯狂摩擦2 | 不卡的毛片| 国产美女精品自拍 | 精品国产一区二区三区不卡 | 亚洲精品一区二区久久这里 | 日韩不卡中文字幕 | 日韩免费在线播放 | 亚洲女同视频 | 日本成aⅴ人片日本伦 | 能看的黄色网址 | 2022国内精品免费福利视频 | 亚洲国产视频一区 | 成年人在线观看视频网站 | 亚洲无毛片 | 欧美另类亚洲一区二区 | 日本一级特黄毛片免费视频9 | 欧美成人777 | 精品视自拍视频在线观看 | 亚洲精品在线影院 | 三级国产在线观看 | 天天综合久久 | 成人欧美一区二区三区视频xxx | 黄色短视频免费 | 成年免费大片黄在线观看岛国 |