还是一样的,最近需要系统三级等保抽查,所以需要对系统进行整改。这篇文章我们主要介绍下在生产环境,我们的数据库密码不应该是用明文配置的,需要使用加密后的密文进行交互。因此这里我们还是在前面文章的例子上直接进行改造,在配置文件里面配置mysql密文,然后测试下程序是否好使。
方案:
这里的方案主要采取的是druid数据库连接池加上druid连接池的秘钥工具进行配置使用。下面介绍下详细的信息。
一、首先我们在maven项目里面添加一个druid的依赖。
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.24</version> </dependency>
二、编写一个工具类,生成公钥、私钥、加解密方法
package com.mybatis.plus.demo.utils; import com.alibaba.druid.filter.config.ConfigTools; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; /** * alibaba druid加解密规则: 明文密码+私钥(privateKey)加密=加密密码 加密密码+公钥(publicKey)解密=明文密码 */ @Slf4j public class DruidEncryptorUtils { private static String privateKey; private static String publicKey; static { try { String[] keyPair = ConfigTools.genKeyPair(512); privateKey = keyPair[0]; log.info("生成的私钥是:{}",privateKey); publicKey = keyPair[1]; log.info("生成的公钥是:{}",publicKey); } catch (Exception e) { log.error(e.getMessage(), e); } } /** * 明文加密 * * @param plaintext * @return */ @SneakyThrows public static String encode(String plaintext) { log.info("明文字符串:{} ", plaintext); String ciphertext = ConfigTools.encrypt(privateKey, plaintext); log.info("加密后字符串:{}", ciphertext); return ciphertext; } /** * 解密 * * @param ciphertext * @return */ @SneakyThrows public static String decode(String ciphertext) { log.info("加密字符串:{}", ciphertext); String plaintext = ConfigTools.decrypt(publicKey, ciphertext); log.info("解密后的字符串:{}", plaintext); return plaintext; } }
三、我们编写一个test把数据库的密码,打印出加密后的密文。
package com.mybatis.plus.demo.test; import com.mybatis.plus.demo.utils.DruidEncryptorUtils; public class DruidPasswordTest { public static void main(String[] args) { DruidEncryptorUtils utils = new DruidEncryptorUtils(); String encode = utils.encode("123456"); System.out.println("加密后的密码是:"+encode); } }
四、测试下这个test,然后把公钥和加密后的密码记录下来。
五、修改我们的web项目数据库连接的信息,把url,uername,password改写掉,同时把公钥复制进去。
url: ${DATASOURCE_URL:jdbc:mysql://192.168.31.30:3307/test?useUnicode=true&useSSL=false&characterEncoding=utf8} #自己的数据库名称 username: ${DATASOURCE_USERNAME:root} password: ${DATASOURCE_PWD:LBBguAdh2VAxpL5Fo2tS86fHNTH17VWKe93IrBaEBj4M2qysVuv4wIHlzB3+o3pYPOYngfa3MEin9bTX893apQ==} publickey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJXz4+TwUWl1Ddx3cRBEqZq3Yl5hmIb0t2Zp2kvmNd4H9gPxsM7WAKw5CYURSVFrGB00EzvmcjuIDxLo3DTnPd0CAwEAAQ==
完整的application.yml内容是:
#端口号 server: port: 8088 #数据库的配置信息 spring: mybatis: #开启驼峰命名法 configuration: map-underscore-to-camel-case: true mybatis-plus: # xml地址 mapper-locations: classpath:mapper/*Mapper.xml # 实体扫描,多个package用逗号或者分号分隔 type-aliases-package: com.example.mybatisplus.entity #自己的实体类地址 configuration: # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl datasource: type: com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.jdbc.Driver url: ${DATASOURCE_URL:jdbc:mysql://192.168.31.30:3307/test?useUnicode=true&useSSL=false&characterEncoding=utf8} #自己的数据库名称 username: ${DATASOURCE_USERNAME:root} password: ${DATASOURCE_PWD:LBBguAdh2VAxpL5Fo2tS86fHNTH17VWKe93IrBaEBj4M2qysVuv4wIHlzB3+o3pYPOYngfa3MEin9bTX893apQ==} publickey: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJXz4+TwUWl1Ddx3cRBEqZq3Yl5hmIb0t2Zp2kvmNd4H9gPxsM7WAKw5CYURSVFrGB00EzvmcjuIDxLo3DTnPd0CAwEAAQ== druid: # 初始连接数 initialSize: 5 # 最小连接池数量 minIdle: 10 # 最大连接池数量 maxActive: 20 # 配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 # 配置一个连接在池中最大生存的时间,单位是毫秒 maxEvictableIdleTimeMillis: 900000 # 配置检测连接是否有效 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false webStatFilter: enabled: true statViewServlet: enabled: true # 设置白名单,不填则允许所有访问 allow: url-pattern: /druid/* # 控制台管理用户名和密码 login-username: login-password: filter: stat: enabled: true # 慢SQL记录 log-slow-sql: true slow-sql-millis: 1000 merge-sql: true wall: config: multi-statement-allow: true config: enabled: true connection-properties: config.decrypt=true;config.decrypt.key=${spring.datasource.publickey}
备注:这里大家可以看到我填写的内容和测试的结果内容不一致,这是因为我是把这个demo项目写完后,测试完毕再编写的这篇文章,真实的情况,大家各自替换即可。
六、启动项目,测试下访问
请求:http://localhost:8088/user/getIdCard?IdCard=123456789012345678
可以看到有正确的数据返回。
这里我们就能看到测试成功了。达到了以下目的:
1、数据库密码是密文配置。 2、程序连接mysql没有任何问题,可以正常访问
最后附上完整代码下载:下载
还没有评论,来说两句吧...