收藏 分享(赏)

SpringBoot整合Druid实现数据库密码加密.pdf

上传人:明湖 文档编号:3938524 上传时间:2021-05-16 格式:PDF 页数:4 大小:701.03KB
下载 相关 举报
SpringBoot整合Druid实现数据库密码加密.pdf_第1页
第1页 / 共4页
SpringBoot整合Druid实现数据库密码加密.pdf_第2页
第2页 / 共4页
SpringBoot整合Druid实现数据库密码加密.pdf_第3页
第3页 / 共4页
SpringBoot整合Druid实现数据库密码加密.pdf_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

1、SpringBoot(七)SpringBoot整合Druid实现数据 库密码加密 SpringBoot(七)SpringBoot整合Druid实现数据库密码加密 1、新建一个Maven项目 1.1、项目结构树 1.2、项目结构图 2、创建加解密程序 2.1、DecryptDruid 2.2、生成公钥和密码 3、配置我们的项目 3.1、配置application.properties文件 3.2、启动项目并验证 我们在实际的生产环境中为了防止数据库的密码被直接读取,经常需要将密码加密存储; 本文介绍了使用SpringBoot整合Druid时,将密码使用非对称的方式进行加密的过程 用以防止密码被人

2、直接窃取 1、新建一个Maven项目 使用IDEA按照提示,创建一个普通的maven项目,如下: 1.1、项目结构树 1.2、项目结构图 druid_decrypt.iml pom.xml src main java test java target 2、创建加解密程序 2.1、DecryptDruid 编写一个类DecryptDruid,里面需要包含一个加密方法testEncrypt和一个解密方法 testDecrypt, 并增加一个main方法,生成公钥和密文密码 全部代码如下: package com.iambest.study; import com.alibaba.druid.fil

3、ter.config.ConfigTools; /* * * Druid的公钥 * * author zhang_wei * version 1.0.0 * Classname DecryptDruid * Date 2021/3/7 20:37 * Created by zhang_wei * since 1.0.0 */ public class DecryptDruid public static void main(String args) throws Exception testEncrypt(123456); 2.2、生成公钥和密码 运行DecryptDruid的main方法,查

4、看控制台输出 这里可以正常的输出公钥、密文的密码 解密也可以正常的解密成功 表示正常执行 /* * 对指定的密码使用公钥进行解密 * * param passwd 加密后的密码 * param publicKey 公钥 * return 解密后的明文密码 * throws Exception 異常 */ public static String testDecrypt(String passwd, String publicKey) throws Exception / 解密 String decryptPassword = ConfigTools.decrypt(publicKey, pas

5、swd); System.out.println(decryptPassword: + decryptPassword); return decryptPassword; /* * 对指定的明文密码,生成公私钥进行加密 * * param password 明文密码 * return 密文密码 * throws Exception 異常 */ public static String testEncrypt(String password) throws Exception String keyPair = ConfigTools.genKeyPair(512); /私钥 String pri

6、vateKey = keyPair0; /公钥 String publicKey = keyPair1; /加密 password = ConfigTools.encrypt(privateKey, password); System.out.println(privateKey: + privateKey); System.out.println(publicKey: + publicKey); System.out.println(password: + password); / 解密操作 testDecrypt(password, publicKey); return password;

7、 3、配置我们的项目 3.1、配置application.properties文件 修改密码这里,将密码改为密文,增加一个公钥publickey 调整spring.datasource.druid.filters增加一个config配置项 调整spring.datasource.druid.connection-properties在后面追加config的配置 完整的配置如下: 3.2、启动项目并验证 我们启动自己的项目,查看控制台的输出 可以看到我们的数据库连接池正常的 spring.datasource.url=jdbc:mysql:/localhost:3306/david? useUnicode=truedruid.stat.slowSqlMillis=5000;config.decrypt=true; config.decrypt.key=$publickey

展开阅读全文
相关资源
相关搜索
资源标签

当前位置:首页 > 网络技术 > 程序设计

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:文库网官方知乎号:文库网

经营许可证编号: 粤ICP备2021046453号世界地图

文库网官网©版权所有2025营业执照举报