欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于properties配置文件的加密方式

 更新時(shí)間:2022年10月26日 11:02:48   作者:lxcoding  
這篇文章主要介紹了關(guān)于properties配置文件的加密方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

要完成properties屬性文件某些屬性值的加密,和讀取屬性文件時(shí)進(jìn)行解密

需要4個(gè)步驟

  • 編寫加密解密工具類
  • 手動(dòng)通過加密解密工具類獲得加密后的屬性值密文,并把密文填寫在properties文件中
  • 編寫PropertyPlaceholderConfigurer的子類,重寫convertProperty()方法
  • 在spring-dao.xml配置文件中配置PropertyPlaceholderConfigurer類

接下來我們將拿配置數(shù)據(jù)庫的properties文件進(jìn)行舉例(一般我們需要對用戶名和密碼進(jìn)行加密)

編寫加密解密工具類

在編寫工具類前我們需要導(dǎo)入包含Base64這個(gè)類的依賴

<dependency>
?? ?<groupId>commons-codec</groupId>
?? ?<artifactId>commons-codec</artifactId>
?? ?<version>1.14</version>
</dependency>

之所以要使用Base64對加密后的byte數(shù)組進(jìn)行編碼,可以參考Base64編碼及其作用

編寫使用DES加密算法的加密解密工具類

package com.lxc.o2o.util;
import java.io.UnsupportedEncodingException;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import org.apache.commons.codec.binary.Base64;

/**
?* DES是一種對稱加密算法,所謂對稱加密算法即:加密和解密使用相同密鑰的算法。
?*?
?*/
public class DESUtil {

?? ?// 秘鑰對象
?? ?private static Key key;
?? ?// 設(shè)置密鑰key
?? ?private static String KEY_STR = "myKey";
?? ?// 使用的編碼
?? ?private static String CHARSETNAME = "UTF-8";
?? ?// 設(shè)置使用DES算法(我們這里主要使用java的DES算法)
?? ?private static String ALGORITHM = "DES";

?? ?// 初始化秘鑰對象key
?? ?static {
?? ??? ?try {
?? ??? ??? ?// 生成DES算法對象
?? ??? ??? ?KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
?? ??? ??? ?// 運(yùn)用SHA1安全策略
?? ??? ??? ?SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
?? ??? ??? ?// 設(shè)置上密鑰種子
?? ??? ??? ?secureRandom.setSeed(KEY_STR.getBytes());
?? ??? ??? ?// 初始化基于SHA1的算法對象
?? ??? ??? ?generator.init(secureRandom);
?? ??? ??? ?// 生成密鑰對象
?? ??? ??? ?key = generator.generateKey();
?? ??? ??? ?generator = null;
?? ??? ?} catch (Exception e) {
?? ??? ??? ?throw new RuntimeException(e);
?? ??? ?}
?? ?}

?? ?/**
?? ? * 獲取加密后的信息
?? ? *?
?? ? * @param str
?? ? * @return
?? ? */
?? ?public static String getEncryptString(String str) {

?? ??? ?try {
?? ??? ??? ?// 按UTF8編碼
?? ??? ??? ?byte[] bytes = str.getBytes(CHARSETNAME);
?? ??? ??? ?// 獲取加密對象
?? ??? ??? ?Cipher cipher = Cipher.getInstance(ALGORITHM);
?? ??? ??? ?// 初始化密碼信息,Cipher.ENCRYPT_MODE為加密類型
?? ??? ??? ?cipher.init(Cipher.ENCRYPT_MODE, key);
?? ??? ??? ?// 加密
?? ??? ??? ?byte[] doFinal = cipher.doFinal(bytes);
?? ??? ??? ?// 基于BASE64編碼,接收byte[]并轉(zhuǎn)換成String
?? ??? ??? ?// byte[]to encode好的String并返回,編碼成字符串返回
?? ??? ??? ?return Base64.encodeBase64String(doFinal);
?? ??? ?} catch (Exception e) {
?? ??? ??? ?// TODO: handle exception
?? ??? ??? ?throw new RuntimeException(e);
?? ??? ?}
?? ?}

?? ?/**
?? ? * 獲取解密之后的信息
?? ? *?
?? ? * @param str
?? ? * @return
?? ? */
?? ?public static String getDecryptString(String str) {

?? ??? ?try {
?? ??? ??? ?// 基于BASE64編碼,接收byte[]并轉(zhuǎn)換成String
?? ??? ??? ?// 將字符串decode成byte[],解碼操作
?? ??? ??? ?byte[] bytes = Base64.decodeBase64(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) {
?? ??? ??? ?// TODO: handle exception
?? ??? ??? ?throw new RuntimeException(e);
?? ??? ?}
?? ?}

?? ?public static void main(String[] args) throws UnsupportedEncodingException {
?? ??? ?System.out.println(getEncryptString("root"));
?? ??? ?System.out.println(getEncryptString("123456"));

?? ?}

}

獲取用戶名和密碼的秘文

通過上面編寫的DESUtil獲取用戶名和密碼的密文,再把密文填寫進(jìn)jdbc.properties文件

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/myo2o?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
jdbc.username=WnplV/ietfQ=
jdbc.password=QAHlVoUc49w=

編寫PropertyPlaceholderConfigurer的子類

package com.lxc.o2o.util;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
?* 獲取解密后的屬性值
?*?
?* @author L7832
?*
?*/
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
?? ?// 需要加密的字段數(shù)組(這里整個(gè)jdbc屬性文件,我們只對username和password加密了)
?? ?private String[] encryptPropNames = { "jdbc.username", "jdbc.password" };

?? ?/**
?? ? * 對關(guān)鍵的屬性進(jìn)行轉(zhuǎn)換,重寫PropertyPlaceholderConfigurer中的convertProperty方法
?? ? * 這個(gè)函數(shù)會對屬性文件中所有的屬性鍵值對進(jìn)行讀取
?? ? */
?? ?@Override
?? ?protected String convertProperty(String propertyName, String propertyValue) {
?? ??? ?// 判斷屬性值是否被加密了
?? ??? ?if (isEncryptProp(propertyName)) {
?? ??? ??? ?// 對已加密的字段進(jìn)行解密工作
?? ??? ??? ?String decryptValue = DESUtil.getDecryptString(propertyValue);
?? ??? ??? ?return decryptValue;
?? ??? ?} else {
?? ??? ??? ?// 如果沒被加密,直接返回
?? ??? ??? ?return propertyValue;
?? ??? ?}
?? ?}
?? ?/**
?? ? * 判斷該屬性是否已加密,主要拿傳進(jìn)來的屬性名和上面我們定義的需要加密的字段數(shù)組進(jìn)行比對
?? ? *?
?? ? * @param propertyName
?? ? * @return
?? ? */
?? ?private boolean isEncryptProp(String propertyName) {
?? ??? ?// 若等于需要加密的field,則進(jìn)行加密
?? ??? ?for (String encryptpropertyName : encryptPropNames) {
?? ??? ??? ?if (encryptpropertyName.equals(propertyName))
?? ??? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
}

配置Bean

在spring-dao.xml配置文件中配置我們自己實(shí)現(xiàn)的EncryptPropertyPlaceholderConfigurer類

<!--連接數(shù)據(jù)庫時(shí),會自動(dòng)讀取對應(yīng)的配置文件,并進(jìn)行解密操作-->
<bean class="com.lxc.o2o.util.EncryptPropertyPlaceholderConfigurer">
?? ?<property name="locations">
?? ??? ?<list>
?? ??? ??? ?<value>classpath:jdbc.properties</value>
?? ??? ??? ?<!-- 如果要讀取其他加密的配置文件,繼續(xù)配置在這個(gè)list中 -->
?? ??? ?</list>
?? ?</property>
?? ?<property name="fileEncoding" value="UTF-8"></property>
</bean>

運(yùn)行時(shí)創(chuàng)建了上面的bean后,可以直接通過${屬性名}獲取解密后的屬性值

<!-- 2.數(shù)據(jù)庫連接池 ?-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
?? ?<!--配置連接池屬性 -->
?? ?<property name="driverClass" value="${jdbc.driver}"></property>
?? ?<property name="jdbcUrl" value="${jdbc.url}"></property>
?? ?<property name="user" value="${jdbc.username}"></property>
?? ?<property name="password" value="${jdbc.password}"></property>?? ?
</bean>

到這里我們就完成了加密和解密properties文件的所有操作 

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論