MyBatis-Flex数据源密码加密使用

数据源加密指的是我们对数据库的链接信息进行加密,目前 MyBatis-Flex 支持加密的内容有

  • 数据源 URL
  • 数据源用户名
  • 数据源用户密码

通过数据源加密功能,我们可以有效的保证数据库安全,减少数据盗用风险。

简介

在 MyBatis-Flex 中,我们并不关注数据库信息的加密方式,换一句话也就是说:MyBatis-Flex 支持任何类型的加密方式

在 MyBatis-Flex 中内置了一个名为 DataSourceDecipher 的接口,其作用是去解密用户配置的加密内容,从而真实的配置信息。

SpringBoot 示例-(使用RSA加解密的方式使用)

1、自定义配置:

spring:

  datasource:

    url: jdbc:mysql://192.168.***.***:3306/yf3b_zhyb

    username: user

    password: QGBWhdD+oPQVu2zRFDjzrIgobve9mL4xC2222J+pk8rfo9ifnj2zAfiTwoB2a4YBx1A+gNL47yoEBgYxlj/W4Fjw==

    decrypt:

      key: MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIZw0FBgkc9+8VBTgLuMc/oq+2YXeZ7U+AGQzB3xl2UJzcYoeLf8IKHLZA0qouxHk35agLalkJ4mi/jinoJ12qcCAwEAAQ==

2、配置解密类

package com.ahhx.data.config;

import com.mybatisflex.core.datasource.DataSourceDecipher;

import com.mybatisflex.core.datasource.DataSourceProperty;

import com.zhsq.verify.util.RSAUtil;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.Profile;

/**

 * 自定义解密数据库密码

 * @author ZHU

 */

@Configuration

@Profile("pro")

@Slf4j

public class DataSourceEncryptionConfig {

    @Value("${spring.datasource.decrypt.key}")

    private String key;

     

    @Bean

    public DataSourceDecipher decipher(){

        DataSourceDecipher decipher = new DataSourceDecipher() {

            @Override

            public String decrypt(DataSourceProperty dataSourceProperty, String value) {

                //解密密码

                if (dataSourceProperty == DataSourceProperty.PASSWORD) {

                    try {

                        return RSAUtil.decryptByPubKey(value, key);

                    catch (Exception e) {

                        log.error("密码解密失败");

                        throw new RuntimeException(e);

                    }

                }

                return value;

            }

        };

        return decipher;

    }

     

}

工具类方法:

/**

 * 公钥解密

 *

 * @param data      解密前的字符串

 * @param publicKey 公钥

 * @return 解密后的字符串

 * @throws Exception

 */

public static String decryptByPubKey(String data, String publicKey) throws Exception {

    byte[] pubKey = decodeBase64(publicKey);

    byte[] design = decryptByPubKey(Base64.getDecoder().decode(data), pubKey);

    return new String(design);

}

/**

 * 密钥转成byte[]

 *

 * @param key

 * @return

 */

public static byte[] decodeBase64(String key) {

    return Base64.getDecoder().decode(key);

}

/**

 * 数字签名,密钥算法

 */

private static final String RSA_KEY_ALGORITHM = "RSA";

/**

 * 数字签名签名/验证算法

 */

private static final String SIGNATURE_ALGORITHM = "MD5withRSA";

/**

 * RSA密钥长度,RSA算法的默认密钥长度是1024密钥长度必须是64的倍数,在512到65536位之间

 */

private static final int KEY_SIZE = 1024;

/**

 * RSA最大加密明文大小

 */

private static final int MAX_ENCRYPT_BLOCK = 117;

/**

 * RSA最大解密密文大小

 */

private static final int MAX_DECRYPT_BLOCK = 128;

/**

 * 公钥解密

 *

 * @param data   待解密的数据

 * @param pubKey 公钥

 * @return 解密后的数据

 * @throws Exception

 */

public static byte[] decryptByPubKey(byte[] data, byte[] pubKey) throws Exception {

    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(pubKey);

    KeyFactory keyFactory = KeyFactory.getInstance(RSA_KEY_ALGORITHM);

    PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);

    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

    cipher.init(Cipher.DECRYPT_MODE, publicKey);

    int inputLen = data.length;

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    int offSet = 0;

    byte[] cache;

    int i = 0;

    // 对数据分段解密

    while (inputLen - offSet > 0) {

        if (inputLen - offSet > MAX_DECRYPT_BLOCK) {

            cache = cipher.doFinal(data, offSet, MAX_DECRYPT_BLOCK);

        else {

            cache = cipher.doFinal(data, offSet, inputLen - offSet);

        }

        out.write(cache, 0, cache.length);

        i++;

        offSet = i * MAX_DECRYPT_BLOCK;

    }

    byte[] decryptedData = out.toByteArray();

    out.close();

    return decryptedData;

}

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值