Druid连接池加密与解密

加密

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
</dependency>

> 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

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

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==

解密

<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
</dependency>
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();
    }
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/19/druid-connection-pool-encryption-and-decryption/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Druid连接池加密与解密
加密 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </depen……
<<上一篇
下一篇>>
文章目录
关闭
目 录