1.依赖

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

yml配置

spring:
  profiles:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/sys?useUnicode=true&charaterEncoding=utf-8&allowMultiQueries=true&autoReconnect=true&serverTimezone=GMT%2B8
    username: sys
    password: C6I97DiEZKvBshupiM7LpkZhsGIgbidS4bar3Pma9g1JomLvHinqIEZUsq6EQqeYOLu2TK8cNwxs5r40sclefw==
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 10
    maxActive: 50
    minIdle: 5
    filters: config
    connection-properties: config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJieFBI7jA1DMtp/kUgiOzzKS/Nd23JcdZjgBDCwsYywNP2dvONjGRzsJU0hKIML3pDLqH1mID/w6Rx4KM+7IkMCAwEAAQ==

 生产公钥 私钥 和密文的代码

import com.alibaba.druid.filter.config.ConfigTools;
import lombok.SneakyThrows;

public final class DruidEncryptorUtils {

    private static String privateKey;

    private static String publicKey;

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey = keyPair[0];
            System.out.println(String.format("私钥加密时使用-->%s", privateKey));
            publicKey = keyPair[1];
            System.out.println(String.format("公钥解密时使用-->%s", publicKey));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 明文加密
     *
     * @param plaintext
     * @return
     */
    @SneakyThrows
    public static String encode(String plaintext, String yourPrivateKey) {
        if (yourPrivateKey == null) {
            yourPrivateKey = privateKey;
        }
        System.out.println("明文字符串:" + plaintext);
        String ciphertext = ConfigTools.encrypt(yourPrivateKey, plaintext);
        System.out.println("加密后字符串:" + ciphertext);
        return ciphertext;
    }

    /**
     * 解密
     *
     * @param ciphertext
     * @return
     */
    @SneakyThrows
    public static String decode(String ciphertext, String yourPublicKey ) {
        if (yourPublicKey == null) {
            yourPublicKey = publicKey;
        }
        System.out.println("加密字符串:" + ciphertext);
        String plaintext = ConfigTools.decrypt(yourPublicKey, ciphertext);
        System.out.println("解密后的字符串:" + plaintext);

        return plaintext;
    }

    public static void main(String[] args) {
        String plaintext = "TransitCenter@99";
        //秘钥为null,就生成秘钥
        String ciphertext = encode(plaintext, null);
        decode(ciphertext, null);
    }

}

如果报错

修改配置试试

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/transitcenter?useUnicode=true&charaterEncoding=utf-8&allowMultiQueries=true&autoReconnect=true&serverTimezone=GMT%2B8
    username: root
    password: bNg4x/EuurFmfNsf/c1ojS9ajP90LsRbxB7wIn5Q7P4GY1Yu2d6jdtqLVGHWBQNsEiaRZr0LCsnSdjS5DJIYXw==
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 10
    maxActive: 50
    minIdle: 5
    druid:
      filter:
        config:
          enabled: true
      connection-properties: config.decrypt=true;config.decrypt.key=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALivG2muOLRkAEND1O7c/mGmIJMwqY0dq3dQf6+AfXG03Jt5AsBioudS7mgViaBITOeadURJTvwRIis+8A8t08UCAwEAAQ==

Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐