Druid连接池加密与解密

加密

1
2
3
4
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
1
2
3
4
> java -cp D:\Libs\MavenRepository\com\alibaba\druid\1.1.10\druid-1.1.10.jar com.alibaba.druid.filter.config.ConfigTools O%akLm@5K8
privateKey:MIIBUwIBADANBgkqhkiG9w0BAQEFAASCAT0wggE5AgEAAkEAlpvmR6VMz4D8kTDCutogzJJ9SFeV7iGUbjLpZ9I87quf0VBKpxxGB1Xb9zcfwfYsYpjVrsqO6Ag9JMREqmj+CwIDAQABAkBYm13u1tyK1EtDrFlUjxJu6NxWXFk0IYNvFfrl/vWkQGIywHULM5E8J+URD1AUf12U25UnNYA4I9lCA2H5wsUJAiEA2zSbe1rKJtMib8ytj5xf73KxKWBoj4QaIDnJlnYfz8cCIQCv46iTERDOV9B8TO10J+auk+MDFgrI4CloarMNrEbnnQIgKB0batp1bd9rSNxC0qoCIFZztyeuWr0I7ubETiV1stcCIHMXEWAiA5AxT792Cs49qalEJYAIdUgZvZ6QatcgikjVAiA5xdGGsHmgXRUbxwlwTlXEorYVOal7Y1WuCz3a8KCXmA==
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJab5kelTM+A/JEwwrraIMySfUhXle4hlG4y6WfSPO6rn9FQSqccRgdV2/c3H8H2LGKY1a7KjugIPSTERKpo/gsCAwEAAQ==
password:D/A+KgvXgFCCGb2wOABZljrk4hzXJnivjTRqkJbGPSJx9z65CcvNbn9Bjuy5NIj52kOaNQ5C5Duto7g8dgsjAQ==

注意:原始密码不能包含&符号,否则会被截断

明文application.yml

1
2
3
4
5
6
7
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.1.10:3306/appblog?useUnicode=true&autoReconnect=true&characterEncoding=utf-8
username: appblog
password: O%akLm@5K8

密文application.yml

1
2
3
4
5
6
7
8
9
10
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.1.10:3306/appblog?useUnicode=true&autoReconnect=true&characterEncoding=utf-8
username: appblog
password: D/A+KgvXgFCCGb2wOABZljrk4hzXJnivjTRqkJbGPSJx9z65CcvNbn9Bjuy5NIj52kOaNQ5C5Duto7g8dgsjAQ==
druid:
filters: config
connection-properties: config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJab5kelTM+A/JEwwrraIMySfUhXle4hlG4y6WfSPO6rn9FQSqccRgdV2/c3H8H2LGKY1a7KjugIPSTERKpo/gsCAwEAAQ==

解密

1
2
3
4
5
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
import com.alibaba.druid.filter.config.ConfigTools;

public static void main(String[] args) {
//解密
String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJab5kelTM+A/JEwwrraIMySfUhXle4hlG4y6WfSPO6rn9FQSqccRgdV2/c3H8H2LGKY1a7KjugIPSTERKpo/gsCAwEAAQ==";
String password = "D/A+KgvXgFCCGb2wOABZljrk4hzXJnivjTRqkJbGPSJx9z65CcvNbn9Bjuy5NIj52kOaNQ5C5Duto7g8dgsjAQ==";
try {
String decrypted = ConfigTools.decrypt(publicKey, password);
System.out.println(decrypted);
} catch (Exception e) {
e.printStackTrace();
}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :