關(guān)于properties配置文件的加密方式
要完成properties屬性文件某些屬性值的加密,和讀取屬性文件時進行解密
需要4個步驟
- 編寫加密解密工具類
- 手動通過加密解密工具類獲得加密后的屬性值密文,并把密文填寫在properties文件中
- 編寫PropertyPlaceholderConfigurer的子類,重寫convertProperty()方法
- 在spring-dao.xml配置文件中配置PropertyPlaceholderConfigurer類
接下來我們將拿配置數(shù)據(jù)庫的properties文件進行舉例(一般我們需要對用戶名和密碼進行加密)
編寫加密解密工具類
在編寫工具類前我們需要導(dǎo)入包含Base64這個類的依賴
<dependency> ?? ?<groupId>commons-codec</groupId> ?? ?<artifactId>commons-codec</artifactId> ?? ?<version>1.14</version> </dependency>
之所以要使用Base64對加密后的byte數(shù)組進行編碼,可以參考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);
?? ??? ??? ?// 運用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獲取用戶名和密碼的密文,再把密文填寫進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ù)組(這里整個jdbc屬性文件,我們只對username和password加密了)
?? ?private String[] encryptPropNames = { "jdbc.username", "jdbc.password" };
?? ?/**
?? ? * 對關(guān)鍵的屬性進行轉(zhuǎn)換,重寫PropertyPlaceholderConfigurer中的convertProperty方法
?? ? * 這個函數(shù)會對屬性文件中所有的屬性鍵值對進行讀取
?? ? */
?? ?@Override
?? ?protected String convertProperty(String propertyName, String propertyValue) {
?? ??? ?// 判斷屬性值是否被加密了
?? ??? ?if (isEncryptProp(propertyName)) {
?? ??? ??? ?// 對已加密的字段進行解密工作
?? ??? ??? ?String decryptValue = DESUtil.getDecryptString(propertyValue);
?? ??? ??? ?return decryptValue;
?? ??? ?} else {
?? ??? ??? ?// 如果沒被加密,直接返回
?? ??? ??? ?return propertyValue;
?? ??? ?}
?? ?}
?? ?/**
?? ? * 判斷該屬性是否已加密,主要拿傳進來的屬性名和上面我們定義的需要加密的字段數(shù)組進行比對
?? ? *?
?? ? * @param propertyName
?? ? * @return
?? ? */
?? ?private boolean isEncryptProp(String propertyName) {
?? ??? ?// 若等于需要加密的field,則進行加密
?? ??? ?for (String encryptpropertyName : encryptPropNames) {
?? ??? ??? ?if (encryptpropertyName.equals(propertyName))
?? ??? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
}配置Bean
在spring-dao.xml配置文件中配置我們自己實現(xiàn)的EncryptPropertyPlaceholderConfigurer類
<!--連接數(shù)據(jù)庫時,會自動讀取對應(yīng)的配置文件,并進行解密操作--> <bean class="com.lxc.o2o.util.EncryptPropertyPlaceholderConfigurer"> ?? ?<property name="locations"> ?? ??? ?<list> ?? ??? ??? ?<value>classpath:jdbc.properties</value> ?? ??? ??? ?<!-- 如果要讀取其他加密的配置文件,繼續(xù)配置在這個list中 --> ?? ??? ?</list> ?? ?</property> ?? ?<property name="fileEncoding" value="UTF-8"></property> </bean>
運行時創(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文件的所有操作
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot+mybatis+Vue實現(xiàn)前后端分離項目的示例
本文主要介紹了SpringBoot+mybatis+Vue實現(xiàn)前后端分離項目的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
mybatis條件構(gòu)造器(EntityWrapper)的使用方式
這篇文章主要介紹了mybatis條件構(gòu)造器(EntityWrapper)的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Arthas在線java進程診斷工具在線調(diào)試神器詳解
Arthas是 Alibaba 開源的Java診斷工具,深受開發(fā)者喜愛。這篇文章主要介紹了Arthas在線java進程診斷工具 在線調(diào)試神器,需要的朋友可以參考下2021-11-11
java多線程開發(fā)之通過對戰(zhàn)游戲?qū)W習(xí)CyclicBarrier
這篇文章給大家分享了關(guān)于java多線程開發(fā)中通過對戰(zhàn)游戲?qū)W習(xí)CyclicBarrier的相關(guān)知識點內(nèi)容,有興趣的朋友們學(xué)習(xí)參考下。2018-08-08
Springboot+Mybatis中typeAliasesPackage正則掃描實現(xiàn)方式
這篇文章主要介紹了Springboot+Mybatis中typeAliasesPackage正則掃描實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07

