加密文件忘記密碼怎么解密_MyBatis 配置文件 用戶密碼加密存儲

properties配置文件

一般是使用properties保存配置文件內容,然后在mybatis配置文件中進行讀取
在resource文件下新建db.properties文件
內容如下

# 數據庫配置文件
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://  /mybatis
username =  
password =

然后,接著把文件放入源碼包中
配置mybatis-config.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!-- 讀取數據庫配置文件 --><properties resource="db.properties"/><!-- 定義別名 --><typeAliases><typeAlias type="com.ming.Role" alias="role"/></typeAliases><!-- 自定義數據處理 --><typeHandlers><typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.ming.Sex"/></typeHandlers><!-- 定義數據庫信息 --><environments default="development"><environment id="development"><!-- jdbc事物管理 --><transactionManager type="JDBC"/><!-- 數據庫鏈接信息 --><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><mappers><mapper resource="RoleMapper.xml"/></mappers>
</configuration>

目錄結構如下

395d24b6332b7993e0264beef47c9735.png

數據庫密碼加密

生產環境的數據庫密碼都為加密密碼,需要在使用的時候,把加密密碼解密成為明文
先創建數據庫密碼類

package com.ming.Util;import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.util.Base64;public class Decode {/*** 生成秘鑰* @param* @return*/public static String generateDecode() throws UnsupportedEncodingException {KeyGenerator keyGen = null;//密鑰生成器try {keyGen = KeyGenerator.getInstance("DES");} catch (NoSuchAlgorithmException e) {e.printStackTrace();}keyGen.init(56);//初始化密鑰生成器SecretKey secretKey = keyGen.generateKey();//生成密鑰byte[] key = secretKey.getEncoded();//密鑰字節數組// 進行base64編碼String encodedKey = Base64.getEncoder().encodeToString(key);return encodedKey;}/*** 進行加密* @param string* @param key* @return*/public static String encryptionDecode(String string, String key){//System.out.println(System.getenv("KEYWORDES"));SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), "DES");//恢復密鑰Cipher cipher = null;//Cipher完成加密或解密工作類try {cipher = Cipher.getInstance("DES");} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (NoSuchPaddingException e) {e.printStackTrace();}try {cipher.init(Cipher.ENCRYPT_MODE, secretKey);//對Cipher初始化,加密模式} catch (InvalidKeyException e) {e.printStackTrace();}byte[] cipherByte = null;try {cipherByte = cipher.doFinal(Base64.getDecoder().decode(string));//加密data} catch (IllegalBlockSizeException e) {e.printStackTrace();} catch (BadPaddingException e) {e.printStackTrace();}return Base64.getEncoder().encodeToString(cipherByte);}public static String decryptDecode(String string, String key){SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), "DES");//恢復密鑰Cipher cipher = null;//Cipher完成加密或解密工作類try {cipher = Cipher.getInstance("DES");} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (NoSuchPaddingException e) {e.printStackTrace();}try {cipher.init(Cipher.DECRYPT_MODE, secretKey);//對Cipher初始化,解密模式} catch (InvalidKeyException e) {e.printStackTrace();}byte[] cipherByte = new byte[0];//解密datatry {cipherByte = cipher.doFinal(Base64.getDecoder().decode(string));} catch (IllegalBlockSizeException e) {e.printStackTrace();} catch (BadPaddingException e) {e.printStackTrace();}return Base64.getEncoder().encodeToString(cipherByte);}
}

該類有三個方法,為加密data,解密data,生成key
然后編輯操作系統環境變量
達到輸入

?  ~ echo $KEYWORDES

可以輸出環境變量
接著再次修改SqlSessionFactoryUtil類

package com.ming.Util;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;/*** @author ming* 構建SqlSessionFactory* 由于數據庫連接是寶貴的,需要對數據庫連接統一管理,所以使用單例進行管理* 這里的單利使用的雙重鎖* SqlSessionFactory為線程不安全類型需要加鎖,確保同一時刻,只有一個線程可以使用該對象*/
public class SqlSessionFactoryUtil {/*** SqlSessionFactory對象*/private static SqlSessionFactory sqlSessionFactory = null;/*** 類線程鎖*/private static final Class CLASS_LOCK = SqlSessionFactoryUtil.class;/*** 日志管理類*/private static final Logger logger = LogManager.getLogger();/*** 單例*/private SqlSessionFactoryUtil(){}/*** @return SqlSessionFactory* 初始化SqlSessionFactory對象*/public static SqlSessionFactory initSqlSessionFactory(){// 獲得輸入流InputStream cfgStream = null;// 閱讀流Reader cfgReader = null;InputStream proStream = null;Reader proReader = null;// 持久化屬性集Properties properties = null;try{// 配置文件流cfgStream = Resources.getResourceAsStream("mybatis-config.xml");// 獲得閱讀流cfgReader = new InputStreamReader(cfgStream);// 讀入屬性文件proStream = Resources.getResourceAsStream("db.properties");proReader = new InputStreamReader(proStream);// 持久化屬性集properties = new Properties();// 流轉載進入屬性集合properties.load(proReader);}catch (Exception e){logger.error(e);}if(sqlSessionFactory == null){synchronized (CLASS_LOCK){sqlSessionFactory = new SqlSessionFactoryBuilder().build(cfgReader, properties);}}return sqlSessionFactory;}/*** 打開SqlSession* @return SqlSession*/public static SqlSession openSqlSesion(){// 判空處理if(sqlSessionFactory == null){initSqlSessionFactory();}return sqlSessionFactory.openSession();}
}

接著,再次對密碼進行加密,在讀取的時候,對閱讀流的結果集進行持久化設置
先對db.properties數據庫密碼進行加密
更改以后配置文件如下

# 數據庫配置文件
driver = com.mysql.cj.jdbc.Driver
url = jdbc:mysql://47.94.95.84:32786/mybatis
username = mybatis
password = 8GgwaJCtTXLGItiYF9c4mg==

接著再次更改Util類

package com.ming.Util;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;/*** @author ming* 構建SqlSessionFactory* 由于數據庫連接是寶貴的,需要對數據庫連接統一管理,所以使用單例進行管理* 這里的單利使用的雙重鎖* SqlSessionFactory為線程不安全類型需要加鎖,確保同一時刻,只有一個線程可以使用該對象*/
public class SqlSessionFactoryUtil {/*** SqlSessionFactory對象*/private static SqlSessionFactory sqlSessionFactory = null;/*** 類線程鎖*/private static final Class CLASS_LOCK = SqlSessionFactoryUtil.class;/*** 日志管理類*/private static final Logger logger = LogManager.getLogger();/*** 單例*/private SqlSessionFactoryUtil(){}/*** @return SqlSessionFactory* 初始化SqlSessionFactory對象*/public static SqlSessionFactory initSqlSessionFactory(){// 獲得輸入流InputStream cfgStream = null;// 閱讀流Reader cfgReader = null;InputStream proStream = null;Reader proReader = null;// 持久化屬性集Properties properties = null;try{// 配置文件流cfgStream = Resources.getResourceAsStream("mybatis-config.xml");// 獲得閱讀流cfgReader = new InputStreamReader(cfgStream);// 讀入屬性文件proStream = Resources.getResourceAsStream("db.properties");proReader = new InputStreamReader(proStream);// 持久化屬性集properties = new Properties();// 流裝載進入屬性集合properties.load(proReader);// 獲取當前系統ENVString key = System.getenv("KEYWORDES");// 進行解密properties.setProperty("password", Decode.decryptDecode(properties.getProperty("password"), key));}catch (Exception e){logger.error(e);}if(sqlSessionFactory == null){synchronized (CLASS_LOCK){sqlSessionFactory = new SqlSessionFactoryBuilder().build(cfgReader, properties);}}return sqlSessionFactory;}/*** 打開SqlSession* @return SqlSession*/public static SqlSession openSqlSesion(){// 判空處理if(sqlSessionFactory == null){initSqlSessionFactory();}return sqlSessionFactory.openSession();}
}

書寫單元測試

package com.ming.Util;import org.junit.Test;import static org.junit.Assert.*;public class SqlSessionFactoryUtilTest {@Testpublic void initSqlSessionFactory() {}@Testpublic void openSqlSesion() {SqlSessionFactoryUtil.openSqlSesion();}
}

目前的目錄結構

77ed8d2b4437f950d4a8d4f8b723eaba.png

此時執行單元測試,可以發現單元測試已經通過
控制臺打印出log信息

2019-04-11 17:17:37.357 [DEBUG] org.apache.ibatis.logging.LogFactory.setImplementation(LogFactory.java:105) - Logging initialized using 'class org.apache.ibatis.logging.log4j2.Log4j2Impl' adapter.
2019-04-11 17:17:37.403 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:334) - PooledDataSource forcefully closed/removed all connections.
2019-04-11 17:17:37.403 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:334) - PooledDataSource forcefully closed/removed all connections.
2019-04-11 17:17:37.404 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:334) - PooledDataSource forcefully closed/removed all connections.
2019-04-11 17:17:37.404 [DEBUG] org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(PooledDataSource.java:334) - PooledDataSource forcefully closed/removed all connections.Process finished with exit code 0

發現錯誤,修改加密類

package com.ming.Util;import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.util.Base64;public class Decode {/*** 生成秘鑰* @param* @return*/public static String generateDecode() throws UnsupportedEncodingException {KeyGenerator keyGen = null;//密鑰生成器try {keyGen = KeyGenerator.getInstance("DES");} catch (NoSuchAlgorithmException e) {e.printStackTrace();}keyGen.init(56);//初始化密鑰生成器SecretKey secretKey = keyGen.generateKey();//生成密鑰byte[] key = secretKey.getEncoded();//密鑰字節數組// 進行base64編碼String encodedKey = Base64.getEncoder().encodeToString(key);return encodedKey;}/*** 進行加密* @param string* @param key* @return*/public static String encryptionDecode(String string, String key){SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), "DES");//恢復密鑰Cipher cipher = null;//Cipher完成加密或解密工作類try {cipher = Cipher.getInstance("DES");} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (NoSuchPaddingException e) {e.printStackTrace();}try {cipher.init(Cipher.ENCRYPT_MODE, secretKey);//對Cipher初始化,加密模式} catch (InvalidKeyException e) {e.printStackTrace();}byte[] cipherByte = null;try {cipherByte = cipher.doFinal(string.getBytes());//加密data} catch (IllegalBlockSizeException e) {e.printStackTrace();} catch (BadPaddingException e) {e.printStackTrace();}return Base64.getEncoder().encodeToString(cipherByte);}/*** 進行解密* @param string* @param key* @return*/public static String decryptDecode(String string, String key){SecretKey secretKey = new SecretKeySpec(Base64.getDecoder().decode(key), "DES");//恢復密鑰Cipher cipher = null;//Cipher完成加密或解密工作類try {cipher = Cipher.getInstance("DES");} catch (NoSuchAlgorithmException e) {e.printStackTrace();} catch (NoSuchPaddingException e) {e.printStackTrace();}try {cipher.init(Cipher.DECRYPT_MODE, secretKey);//對Cipher初始化,解密模式} catch (InvalidKeyException e) {e.printStackTrace();}byte[] cipherByte = new byte[0];//解密datatry {cipherByte = cipher.doFinal(Base64.getDecoder().decode(string));} catch (IllegalBlockSizeException e) {e.printStackTrace();} catch (BadPaddingException e) {e.printStackTrace();}return new String(cipherByte);}
}

再次運行,可以發現已經成功執行sql語句

d2ccd55387cf9789de3083ae999330f5.png

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/276571.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/276571.shtml
英文地址,請注明出處:http://en.pswp.cn/news/276571.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

PHP,如何防止同一用戶同一時間多次登錄

轉載鏈接&#xff1a;http://blog.sina.com.cn/s/blog_4832ea590101djnp.html PHP&#xff0c;如何防止同一用戶同一時間多次登錄&#xff1f; 創建表 username password sessionId 張三 123456 ksw9dkw9ksl92w3 備注&#xff1a;用戶…

科技前沿智能創新 2019北京智能家居 全屋智能博覽會

2019北京智能家居大型展覽會 2019北京全屋智能家居博覽會報道布展&#xff1a;2019年6月26日-27日 展會開幕&#xff1a;2019年6月28日上午9&#xff1a;00時展會交易&#xff1a;2019年6月28日-30日 展會撤展&#xff1a;2019年6月30日下午14&#xff1a;00時 展覽會在北京市政…

java 容器_我也來聊聊,JAVA容器與迭代器

java的容器與迭代器是一個老生常談的話題了。本文旨在與大家分享一些關于雙向鏈表與迭代器的運用小技巧&#xff0c;并希望本篇文章的內容能夠在項目中給你帶來幫助。Stack與LinkedListStack是一個LIFO(后進先出)的容器。若要在java中定義一個Stack應該怎么辦&#xff1f;也許你…

VS2005調試時變慢解決辦法

vs2005生成代碼以及調試運行時&#xff0c;如果設置斷點系統運行非常緩慢&#xff0c;從網上查閱一些資料后解決&#xff1a; 主要解決辦法是&#xff1a; 打開VS2005菜單項"工具"---->"導入導出設置"----->"重置所有設置" 本文參考:http:…

apache目錄的訪問控制

轉載鏈接&#xff1a;http://blog.sina.com.cn/s/blog_7be8a2150100trml.html 1.根目錄的訪問控制 DocumentRoot "/var/www/html" <Directory /> Options FollowSymLinks AllowOverride None </Directory> 解釋一下&#xff1a; <Dir…

廣東高院駁回快播對深圳市場監管局2.6億罰款案上訴

雷帝網 樂天 12月29日報道據廣東高院官方微信消息&#xff0c;廣東省高級人民法院對深圳市快播科技有限公司&#xff08;簡稱快播&#xff09;訴深圳市市場監督管理局&#xff08;簡稱市場監管局&#xff09;著作權行政處罰糾紛案作出終審宣判&#xff0c;駁回上訴&#xff0c;…

【Vegas原創】恢復Oracle Package的笨方法

imp沒有恢復package的參數&#xff0c;所以&#xff0c;只能用笨辦法&#xff1a;rowsn&#xff0c;只導入表結構和物件。 步驟&#xff1a; 1&#xff0c;dbca新建一個test數據庫&#xff1b; 2&#xff0c;新增user&#xff0c;表空間&#xff0c;給user賦予權限 3&#xff0…

python enumerate函數_關于python中enumerate和zip函數的用法及舉例

關于python中enumerate和zip函數的用法及舉例關于enumerate函數&#xff1a;enumerate函數可以同時返回列表或元組等可迭代對象的下標和內容&#xff0c;但實際上&#xff0c;enumerate函數實際返回的是一個enumerate類型的可迭代對象&#xff0c;下面是用法舉例&#xff1a;se…

php 解析xml 的四種方法(轉)

轉載鏈接&#xff1a;http://www.cnblogs.com/likwo/archive/2011/08/24/2151793.html XML處理是開發過程中經常遇到的&#xff0c;PHP對其也有很豐富的支持&#xff0c;本文只是對其中某幾種解析技術做簡要說明&#xff0c;包括&#xff1a;Xml parser, SimpleXML, XMLReader,…

Golang 微服務系列 go-kit(Log,Metrics,Tracing)

go-kit Log & Metrics & Tracing 微服務監控3大核心 Log & Metrics & Tracing Log Log 模塊源碼有待分析&#xff08;分析完再補上&#xff09; Metrics 主要是封裝 Metrics 接口&#xff0c;及各個 Metrics(Prometheus,InfluxDB,StatsD,expvar) 中間件的封裝。…

GDI+

載解壓GDI開發包&#xff1b; 2&#xff0e; 正確設置include & lib 目錄&#xff1b; 3&#xff0e; stdafx.h 添加&#xff1a; #ifndef ULONG_PTR #define ULONG_PTR unsigned long* #endif #include <gdiplus.h> 4&#xff0e; 程序中添加GDI的包含文件gdip…

shell 練習3

1、編寫腳本/root/bin/createuser.sh&#xff0c;實現如下功能&#xff1a;使用一個用戶名做為參數&#xff0c;如果指定參數的用戶存在&#xff0c;就顯示其存在&#xff0c;否則添加之&#xff1b;顯示添加的用戶的id號等信息2、編寫腳本/root/bin/yesorno.sh&#xff0c;提示…

HTML5文件實現拖拽上傳

轉載鏈接&#xff1a;http://www.cnblogs.com/caonidayecnblogs/archive/2010/09/09/1821925.html 通過HTML的文件API &#xff0c;Firefox、Chrome等瀏覽器已經支持從操作系統直接拖拽文件&#xff0c;并上傳到服務器。 相對于使用了十多年的HTML表單&#xff0c;這是一個革命…

兩個數組結果相減_學點算法(三)——數組歸并排序

今天來學習歸并排序算法。分而治之歸并算法的核心思想是分而治之&#xff0c;就是將大問題轉化為小問題&#xff0c;在解決小問題的基礎上&#xff0c;再去解決大問題。將這句話套用到排序中&#xff0c;就是將一個大的待排序區間分為小的待排序區間&#xff0c;對小的排序區間…

python實習生面試題_大數據分析實習生面試題庫

原標題&#xff1a;大數據分析實習生面試題庫大數據分析是一個有吸引力的領域&#xff0c;因為它不僅有利可圖&#xff0c;而且您有機會從事有趣的項目&#xff0c;而且您總是在學習新事物。如果您想從頭開始&#xff0c;請查看大數據分析實習生面試題庫以準備面試要點。大數據…

JavaScript編程語言 基礎 (1)

問題&#xff1a;什么是web前端前端&#xff1a;指界面&#xff0c;計算機&#xff08;PC&#xff09;軟件桌面的界面&#xff1b; 計算機端的瀏覽器界面&#xff1b; 移動端的軟件&#xff08;app&#xff09;界面&#xff1b; 移動端的瀏覽器界面。HtmlcssJavaScript 使用網頁…

shell結合expect寫的批量scp腳本工具

轉載鏈接&#xff1a;http://www.jb51.net/article/34005.htm expect用于自動化地執行linux環境下的命令行交互任務&#xff0c;例如scp、ssh之類需要用戶手動輸入密碼然后確認的任務。有了這個工具&#xff0c;定義在scp過程中可能遇到的情況&#xff0c;然后編寫相應的處理語…

ASP記數器

這兩天有好幾個老的ASP網站要改&#xff0c;其中有要求加記數器&#xff0c;為圖簡單&#xff0c;就用文本文件的形式存儲記數。以前用ifream的形式嵌入&#xff0c;不能很好的控制記數器顯示的風格&#xff0c;現在改進了一下&#xff0c;可以很好的與嵌入板塊風格結合了。把做…

php利用openssl實現RSA非對稱加密簽名

轉載鏈接&#xff1a;http://liuxufei.com/weblog/jishu/376.html 1. 先用php生成一對公鑰和私鑰 $res openssl_pkey_new(); openssl_pkey_export($res,$pri); $d openssl_pkey_get_details($res); $pub $d[key]; var_dump($pri,$pub); 2. 保存好自己的私鑰&#xff0c;把公…

[轉] DevExpress 第三方控件漢化的全部代碼和使用方法

DevExpress.XtraEditors.Controls 此控件包中包含的控件最多&#xff0c;包括文本框&#xff0c;下拉列表&#xff0c;按鈕&#xff0c;等等 DevExpress.XtraGrid 網格 DevExpress.XtraBars 菜單欄 和 工具欄 DevExpress.XtraNavBar 導航條 DevExpress.XtraPr…