使用Druid配置密码加解密

1.引进Druid包

2.对密码进行加密

public static void main(String[] args) throws Exception{
        String[] arr = ConfigTools.genKeyPair(512);
        System.out.println("privateKey:"+arr[0]);
        System.out.println("publicKey:"+arr[1]);
        String encryptStr = ConfigTools.encrypt(arr[0],"testPassword");
        System.out.println("encryptStr:"+encryptStr);
        System.out.println("decryptStr:"+ConfigTools.decrypt(arr[1],encryptStr));
    }

输出如下:

privateKey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAnl2eLgNdjZk3sfwvc9VsQHNHRihDZlEOsb6xF8quxuCJ5Iw4iojbIqLifLAEfNeYJahRaVh/rO54VEnoj3dwZQIDAQABAkAl5tgFwS5xxjrLMUUWHv0yi6SCSP6gjUPMDIyL2ilsVU4EXe65aI5PEvXmhINXm3NgAYMyDGWmt7HFidt4JEhdAiEA4hUIMyZ3Mia7ZtXFGW8Sr45ukD6ifwD/BfPrfuKS7GcCIQCzUo96Jemtbe/bNKEEev6vtBVjKcVdqy3NdkMCWFX9UwIhALd32e+oh5JrXClX0VuSh9dv/smQCs6GQG7UQ4f01saBAiEAgX2j6APKq5cQto1ZnN+oXH7BVvtJ72r2R0EYAHCDn8sCICo+EPmadFhqUjYMnOVSmmupFI8wC4o9MdA0RyiAGyq/
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJ5dni4DXY2ZN7H8L3PVbEBzR0YoQ2ZRDrG+sRfKrsbgieSMOIqI2yKi4nywBHzXmCWoUWlYf6zueFRJ6I93cGUCAwEAAQ==
encryptStr:HrtcxsntPI/DMucqqxi86UX1e+yhMCRKD4RcL5W3QA3qDY4NbgK/fEddOlTWtt4mTo5PhgfQRC1rnaI3OM+UXQ==
decryptStr:testPassword

3.Nacos配置

spring:
  datasource:
    druid:
      password: 密文
      #配置连接属性
      connection-properties: config.decrypt=true;config.decrypt.key=${publickey}
      filter:
        config:
          #因为加解密是通过ConfigFilter实现的,所以这里要开启ConfigFilter
          enabled: true


publicKey: 公钥值

Druid的Gitlab地址

https://github.com/alibaba/druid

上一篇:javaSE.Generic泛型


下一篇:BIO,NIO,AIO 总结