Druid密码加解密
Posted 剑雪封喉r
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Druid密码加解密相关的知识,希望对你有一定的参考价值。
目录
数据库密码加密:
1、配置jar包
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.8</version>
</dependency>
2、生成密码
在本地找到jar包所处目录,如:
打开cmd,定位到所在目录,执行指令:java -cp druid-1.0.26.jar com.alibaba.druid.filter.config.ConfigTools 你的密码(示例为password):
Microsoft Windows [版本 6.3.9600]
(c) 2013 Microsoft Corporation。保留所有权利。
C:\\Users\\xiongfei>cd C:\\Users\\xiongfei\\Desktop\\5code\\apache-maven-3.0.5\\reposito
ry\\com\\alibaba\\druid\\1.2.8
C:\\Users\\xiongfei\\Desktop\\5code\\apache-maven-3.0.5\\repository\\com\\alibaba\\druid\\
1.2.8>java -cp druid-1.2.8.jar com.alibaba.druid.filter.config.ConfigTools passw
ord
privateKey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA6DYnu3t7Vs3GqFQRxxUgM
+LJ/rVMs9g4KZPCexe/OWwx4EEDxoyFkzgwPJCSPfH+G2nrI9bG5EfuvwPz66pqHQIDAQABAkBrVLzAt
66mzK5KF7Gv51/fG7Bp/rrZVdN/xEBcTPmiPDLDnGWWuCbPWqV4plsoNO/izon447sdNau+IumxVt0BA
iEA+yf8k2LY5atZfhBa5ktBPbjyC13/AhqxWf0HBkhEybECIQDssKJ48w0O0mRRQv7Gbc4PHfXShr/nH
+ry1jbey8nWLQIhAJ8IMNy6qJC1MVOr04Mjf8ovg8RC3qhccpSCraICvNvhAiBRxrlr1nXVgCV+2gnSn
w+Js41CPqJBBYsUeaSdl4VVxQIhALFGvS7CSpOb7tRzEj+jAcqlusofFnt++Aw0XVayl8yK
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOg2J7t7e1bNxqhUEccVIDPiyf61TLPYOCmTwn
sXvzlsMeBBA8aMhZM4MDyQkj3x/htp6yPWxuRH7r8D8+uqah0CAwEAAQ==
password:tFiGk4SsutR8olUK3+b2IVnZc0/WQYy+PBgWTxthYrGws1nB5YsvozG9narHb8iufx2+rlH
nt4p4Y4TS6t32iA==
3、修改配置
此时获取到公钥publicKey,私钥privateKey,以及密码password,将加密的password填入publicKey分别填入druid的配置中
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
url: jdbc:mysql://localhost:3306/boost-admin?serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&useSSL=false
username: root
password: QImPIH1+jPtqz7fBEFWl3iXngrmbA8m+n/djyvWuE0OJQXOuvQPJGe8/BqLNn3NO7HioBWNX+XpjbgbaDNSJmw==
# encrypt config
filters: config
connect-properties:
config.decrypt: true
config.decrypt.key: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAO46RHV8MLaC0A1sw15q8dNxBurEmgHwFVP1AvA+3+2mKdDL4uzg7tlM1JdgTjV4QOQ79bs8ba1L4ao2I1vvFR8CAwEAAQ==
mybatis:
mapperLocations: classpath:mapper/*.xml
typeAliasesPackage: com.heartsuit.springbootmybatis.oa.entity
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
数据库密码解密:
- 创建一个测试类(DruidTest)写main方法,继承druid中的DruidPasswordCallback类。
- 在main方法中引用ConfigTools.decrypt(publickey, password)方法,填入已知的公钥和密码。
- 打印结果,看是否与上述明文密码一致
package test; import com.alibaba.druid.filter.config.ConfigTools; import com.alibaba.druid.util.DruidPasswordCallback; /** * @Description: Druid加解密(用一句话描述该文件做什么) * @author: qixiongfei * @date: 2022年4月11日 下午2:33:25 * @version V1.0 */ public class DruidTest extends DruidPasswordCallback public static void main(String[] args) throws Exception String publickey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAO46RHV8MLaC0A1sw15q8dNxBurEmgHwFVP1AvA+3+2mKdDL4uzg7tlM1JdgTjV4QOQ79bs8ba1L4ao2I1vvFR8CAwEAAQ=="; String password = "QImPIH1+jPtqz7fBEFWl3iXngrmbA8m+n/djyvWuE0OJQXOuvQPJGe8/BqLNn3NO7HioBWNX+XpjbgbaDNSJmw=="; String pwd = ConfigTools.decrypt(publickey, password); System.out.println(pwd);
以上是关于Druid密码加解密的主要内容,如果未能解决你的问题,请参考以下文章