当前位置: 首页 > news >正文

二.对配置文件中数据库密码进行加密

代码:

public class DruidEncryptUtils {private static String publicKey;private static  String privateKey;static {try {String[] keyPair= ConfigTools.genKeyPair(512);privateKey = keyPair[0];System.out.println("privateKey:"+privateKey);publicKey = keyPair[1];System.out.println("publicKey:"+publicKey);} catch (NoSuchAlgorithmException e) {} catch (NoSuchProviderException e) {throw new RuntimeException(e);}}/*** 加密* @param plainText* @return* @throws Exception*/public static String encrypt(String plainText) throws Exception {String encrypt = ConfigTools.encrypt(privateKey, plainText);System.out.println("encrypt:" + encrypt);return encrypt;}/*** 解密* @param plainText* @return* @throws Exception*/public static String decrypt(String plainText) throws Exception {String decrypt = ConfigTools.decrypt(publicKey, plainText);System.out.println("decrypt:" + decrypt);return decrypt;}public static void main(String[] args) throws Exception {//私钥加密数据库密码得到加密后的串String aaa = encrypt("数据库密码");System.out.println(aaa);}
}

流程:先用私钥加密数据库密码,得到加密后的串,这个串填到数据库的密码位置,开启druid的解密,用公钥去进行解密。

这里需要三个串:

1.生成的公钥

2.生成的私钥

3.用私钥对密码加密后的串

配置文件代码:

spring:datasource:username: root#私钥==》加密后的串,放这,通过公钥解密password: 加密后的串driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://ip:3306/jc-club?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=truetype: com.alibaba.druid.pool.DruidDataSourcedruid:#连接池初始化连接initial-size: 20#最小空闲数min-idle: 20#最大连接数max-active: 100#最大等待时间max-wait: 60000#解密,放到druid下面,别处解密不到connectionProperties: config.decrypt=true;config.decrypt.key=${publicKey}stat-view-servlet:enabled: trueurl-pattern: /druid/*login-username: adminlogin-password: 123456#慢sqlfilter:stat:enabled: trueslow-sql-millis: 2000log-slow-sql: true#防火墙:drop表危险操作拦截wall:enabled: true#开启后才会解密串config:enabled: true
#公钥
publicKey: 公钥串

相关文章:

  • Java 为什么使用泛型
  • Linux程序开发(十二):线程与多线程同步互斥实现抢票系统
  • 【单片机】STM32F070F6P6 开发指南(一)STM32建立HAL工程
  • VUE2 tab切换导航 展示页面内容(父级子级独立)
  • c语言:模拟strlen(三种方法)最全版本
  • Day03—flask与react实现交互(解决跨域问题)
  • 从容应对亿级QPS访问,Redis还缺少什么?no.29
  • c语言----函数
  • 中间件的概念及示例
  • Android跨进程通信--Binder机制及AIDL是什么?
  • 详解 Cookies 和 WebStorage
  • 软件测试面试题(四)
  • 【经典论文阅读10】MNS采样——召回双塔模型的最佳拍档
  • ffpmeg windows WSl 编译so
  • 【开源】大学生竞赛管理系统 JAVA+Vue+SpringBoot+MySQL
  • [分享]iOS开发-关于在xcode中引用文件夹右边出现问号的解决办法
  • 【译】React性能工程(下) -- 深入研究React性能调试
  • Java|序列化异常StreamCorruptedException的解决方法
  • JavaSE小实践1:Java爬取斗图网站的所有表情包
  • MySQL常见的两种存储引擎:MyISAM与InnoDB的爱恨情仇
  • PHP那些事儿
  • quasar-framework cnodejs社区
  • SpringBoot几种定时任务的实现方式
  • 阿里云购买磁盘后挂载
  • 当SetTimeout遇到了字符串
  • 服务器之间,相同帐号,实现免密钥登录
  • 计算机在识别图像时“看到”了什么?
  • 前端知识点整理(待续)
  • 使用Envoy 作Sidecar Proxy的微服务模式-4.Prometheus的指标收集
  • 事件委托的小应用
  • 微服务框架lagom
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • 主流的CSS水平和垂直居中技术大全
  • 分布式关系型数据库服务 DRDS 支持显示的 Prepare 及逻辑库锁功能等多项能力 ...
  • 教程:使用iPhone相机和openCV来完成3D重建(第一部分) ...
  • ​​​​​​​STM32通过SPI硬件读写W25Q64
  • # include “ “ 和 # include < >两者的区别
  • %3cli%3e连接html页面,html+canvas实现屏幕截取
  • (12)Hive调优——count distinct去重优化
  • (23)mysql中mysqldump备份数据库
  • (4)通过调用hadoop的java api实现本地文件上传到hadoop文件系统上
  • (8)STL算法之替换
  • (a /b)*c的值
  • (回溯) LeetCode 77. 组合
  • (一)UDP基本编程步骤
  • (转)ABI是什么
  • (转)nsfocus-绿盟科技笔试题目
  • ./configure,make,make install的作用(转)
  • .NET精简框架的“无法找到资源程序集”异常释疑
  • .Net中间语言BeforeFieldInit
  • @Transaction注解失效的几种场景(附有示例代码)
  • [2018-01-08] Python强化周的第一天
  • [ABC294Ex] K-Coloring
  • [Angular 基础] - 指令(directives)
  • [AutoSar]BSW_Com02 PDU详解