敏感数据统一国标(SM4)加解密Mybatis-Plus/Mybatis实现方案

该博客介绍了如何利用Hutool库的SM4分组密码算法与Mybatis-Plus结合,实现数据库中敏感数据的加密和解密。通过创建一个自定义的`TestDataTypeHandler`类型处理器,实现了在入库时自动加密和出库时自动解密。同时提供了针对Mybatis-Plus和Mybatis的配置示例,确保数据的安全性。需要注意的是,自定义入参需要手动加密,且加密字段不支持模糊查询。
摘要由CSDN通过智能技术生成

算法背景

SM4分组密码算法是2012年就推出实施的,是我国自主设计的分组对称密码算法,用于实现数据的加密/解密运算,以保证数据和信息的机密性。SM4算法与AES算法具有相同的密钥长度分组长度为128比特,因此在安全性上是高于3DES算法。

具体方案

前提:国标加密工具类尽量不要去重复造轮子,hutool有现成的工具类(当然自己实现也是可以)

<dependency>
  <groupId>cn.hutool</groupId>
  <artifactId>hutool-crypto</artifactId>
  <version>5.7.8</version>
 </dependency>
 <dependency>
   <groupId>org.bouncycastle</groupId>
   <artifactId>bcprov-jdk15to18</artifactId>
   <version>1.69</version>
</dependency>

1创建基于Mybatis的BaseTypeHandler通用处理类

import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SM4;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.nio.charset.StandardCharsets;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @ClassName: TestDataTypeHandler 
 * @Description: mybatis-plus处理敏感数据,应用在string类型上
 * @Author: Simon
 * @Date: 2022/04/21 16:17
 */
@Slf4j
public class TestDataTypeHandler extends BaseTypeHandler<String> {
    /**
     * 16位key
     */
    private static final String SM4_KEY = "aabbccddeeffgghh";

    /**
     * 非空字段加密 - 入库
     * @param preparedStatement
     * @param i
     * @param parameter
     * @param jdbcType
     */
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, String parameter, JdbcType jdbcType) {
        //不处理空字符串
        if(StringUtils.isBlank(parameter)){
            return;
        }
        try {
            SM4 sm4 = SmUtil.sm4(SM4_KEY.getBytes(StandardCharsets.UTF_8));
            String encrypt = sm4.encryptHex(parameter,StandardCharsets.UTF_8);
            log.info("数据:{},加密{}",parameter,encrypt);
            preparedStatement.setString(i, encrypt);
        } catch (Exception e) {
            log.error("typeHandler加密异常:" + e);
        }
    }

    /**
     * 非空字段解密 - 出库
     * @param resultSet
     * @param columnName
     * @return
     * @throws SQLException
     */
    @Override
    public String getNullableResult(ResultSet resultSet, String columnName) throws SQLException {
        String col = resultSet.getString(columnName);
        //不处理空字符串
        if(StringUtils.isBlank(col)){
            return col;
        }
        try {
            //16位key
            SM4 sm4 = SmUtil.sm4(SM4_KEY.getBytes(StandardCharsets.UTF_8));
            String plain = sm4.decryptStr(col,StandardCharsets.UTF_8);
            log.info("数据:{},解密{}",col,plain);
            return plain;
        } catch (Exception e) {
            log.error("数据非sms加密");
        }
        return col;
    }

    /**
     * 可空字段加密
     * @param resultSet
     * @param columnIndex
     * @return
     * @throws SQLException
     */
    @Override
    public String getNullableResult(ResultSet resultSet, int columnIndex) throws SQLException {
        return resultSet.getString(columnIndex);
    }

    /**
     * 可空字段解密
     * @param callableStatement
     * @param columnIndex
     * @return
     * @throws SQLException
     */
    @Override
    public String getNullableResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
        return callableStatement.getString(columnIndex);
    }
}

2.针对于Mybatis-plus用法

  1. 需要在@TableName注解中,设置autoResultMap参数为true
  2. 在需要加解密的字段上,加上注解 @TableField(typeHandler = SensitiveDataTypeHandler.class)
  3. 应用范围:所有MP生成的service与mapper,在进行操作数据的时候会自动应用
@TableName(autoResultMap = true)
public class TestLog extends Model<TestLog> {
  
  	...
    ...
    @TableField(typeHandler = TestDataTypeHandler .class)
    private String mobile;
}

3.针对于Mybatis的用法

  1. SQL注解方式,配置 Result 示例:
 @Select("SELECT * FROM test_log")
 @Results(id= "resultMap", value = {
       @Result(column = "mobile", property = "mobile", typeHandler = TestDataTypeHandler .class)
 })
 List<TestLog> listAll();
  1. XML方式,配置 typeHandler 示例:
<resultMap id="resultMap" type="com.test.TestLog">
    <result column="mobile" property="mobile" typeHandler="com.test.TestDataTypeHandler " />
</resultMap>

<select id="listAll" resultMap="resultMap">
    SELECT * FROM test_log
</select>

注意

  1. 不支持加密自定义入参,自定义入参需要手动加密后入库;(包括自定义查询/保存/修改;推荐使用mybatis-plus操作类实现,可自动处理)
  2. 加密字段不支持模糊查询
  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值