加密文件忘记密码怎么解密_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.mzph.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…