当前位置: 首页 > news >正文

MyBatis拦截器插件:实现敏感数据字段加解密

文章目录

  • 一、写在前面
  • 二、编码实现
    • 1、注解
    • 2、拦截器插件
    • 3、配置插件
    • 4、实体类
    • 5、测试
  • 三、扩展
    • 1、优化点

一、写在前面

日常开发中,经常有一些敏感数据,直接写入数据库的话,很容易泄露。
本文基于mybatis拦截器插件,实现敏感数据的加解密。

二、编码实现

1、注解

import java.lang.annotation.*;@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EncryptedField {String algorithm() default "AES";
}

2、拦截器插件


import com.example.encryption.annotation.EncryptedField;
import com.example.encryption.service.EncryptionService;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.Properties;@Component
@Intercepts({@Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, org.apache.ibatis.session.RowBounds.class, org.apache.ibatis.session.ResultHandler.class})
})
public class EncryptDecryptInterceptor implements Interceptor {@Autowiredprivate EncryptionService encryptionService;@Overridepublic Object intercept(Invocation invocation) throws Throwable {MappedStatement ms = (MappedStatement) invocation.getArgs()[0];Object parameter = invocation.getArgs()[1];// 加密处理 INSERT/UPDATEif (ms.getSqlCommandType() == SqlCommandType.INSERT || ms.getSqlCommandType() == SqlCommandType.UPDATE) {handleEncryption(parameter);}Object result = invocation.proceed();// 解密处理 SELECTif (ms.getSqlCommandType() == SqlCommandType.SELECT) {handleDecryption(result);}return result;}private void handleEncryption(Object parameter) throws Exception {if (parameter == null) return;for (Field field : parameter.getClass().getDeclaredFields()) {if (field.isAnnotationPresent(EncryptedField.class)) {field.setAccessible(true);Object value = field.get(parameter);if (value instanceof String) {field.set(parameter, encryptionService.encrypt((String) value));}}}}private void handleDecryption(Object result) throws Exception {if (result == null) return;if (result instanceof java.util.Collection) {for (Object obj : (java.util.Collection<?>) result) {decryptObject(obj);}} else {decryptObject(result);}}private void decryptObject(Object obj) throws Exception {for (Field field : obj.getClass().getDeclaredFields()) {if (field.isAnnotationPresent(EncryptedField.class)) {field.setAccessible(true);Object value = field.get(obj);if (value instanceof String) {field.set(obj, encryptionService.decrypt((String) value));}}}}@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {}
}

3、配置插件

import com.example.encryption.interceptor.EncryptDecryptInterceptor;
import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MyBatisConfig {@Beanpublic ConfigurationCustomizer configurationCustomizer(EncryptDecryptInterceptor interceptor) {return configuration -> configuration.addInterceptor(interceptor);}
}

4、实体类


import com.example.encryption.annotation.EncryptedField;public class User {private Long id;private String username;@EncryptedFieldprivate String idCard;@EncryptedFieldprivate String phoneNumber;// Getters and Setterspublic Long getId() { return id; }public void setId(Long id) { this.id = id; }public String getUsername() { return username; }public void setUsername(String username) { this.username = username; }public String getIdCard() { return idCard; }public void setIdCard(String idCard) { this.idCard = idCard; }public String getPhoneNumber() { return phoneNumber; }public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; }// toString@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", idCard='" + idCard + '\'' +", phoneNumber='" + phoneNumber + '\'' +'}';}}

5、测试


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;import com.example.encryption.entity.User;
import com.example.encryption.mapper.UserMapper;@SpringBootApplication
public class EncryptionDemoApplication {public static void main(String[] args) {ConfigurableApplicationContext run = SpringApplication.run(EncryptionDemoApplication.class, args);UserMapper userMapper = run.getBean(UserMapper.class);// 增User user = new User();user.setId(1L);user.setUsername("test1");user.setIdCard("111111111111");user.setPhoneNumber("1311111");userMapper.insert(user);System.out.println(userMapper.selectById(1L));;// 改user.setUsername("test2");user.setIdCard("2222222222");user.setPhoneNumber("1322222222");userMapper.updateById(user);System.out.println(userMapper.selectById(1L));;System.out.println(userMapper.selectById(1L));;}
}

在这里插入图片描述

三、扩展

1、优化点

1、插件使用反射对类进行赋值、获取值,为了提高性能,可以考虑将字段进行缓存(使用ConcurrentHashMap)
2、加解密方法,可以考虑扩展成接口,加密方式可扩展。
3、本内容只支持MyBatis简单场景,MyBatisPlus、分页场景、参数为List、Map或者复杂对象,需要对参数进一步递归处理。

http://www.dtcms.com/a/292381.html

相关文章:

  • 中国科技信息杂志中国科技信息杂志社中国科技信息编辑部2025年第14期目录
  • 「芯生态」杰发科技AC7870携手IAR开发工具链,助推汽车电子全栈全域智能化落地
  • Vue中最简单的PDF引入方法及优缺点分析
  • docker build 和compose 学习笔记
  • CASB架构:了解正向代理、反向代理和API扫描
  • [转]Rust:过程宏
  • JMeter 实现 Protobuf 加密解密
  • AI 音频产品开发模板及流程(一)
  • 网络安全第三次作业搭建前端页面并解析
  • allegro 16.6配置CIS库报错 ORCIS-6129 ORCIS-6469
  • LeetCode 658.找到K个最接近的元素
  • .NET使用EPPlus导出EXCEL的接口中,文件流缺少文件名信息
  • Unity笔记——事件中心
  • 力扣-300.最长递增子序列
  • 以太坊网络发展分析:技术升级与市场动态的双重驱动
  • 快手开源 Kwaipilot-AutoThink 思考模型,有效解决过度思考问题
  • Cy3-COOH 花菁染料Cy3-羧基
  • linux-日志服务
  • Gitlab-CI实现组件自动推送
  • 常用 Flutter 命令大全:从开发到发布全流程总结
  • 检索增强型生成助力无人机精准数学推理!RAG-UAV:基于RAG的复杂算术推理方法
  • Lua语言
  • MybatisPlus-16.扩展功能-枚举处理器
  • ORACLE DATABASE 11.2.0.4 RAC Install
  • Vue-22-通过flask接口提供的数据使用plotly.js绘图(一)
  • Oracle定时清理归档日志
  • RAG(检索增强生成)里的文档管理
  • 二次供水管理系统:远程监控+智能调控+故障预警解决方案
  • 用ffmpeg 进行视频的拼接
  • 【指南】网络安全领域:HW 行动(国家网络安全攻防演练)是什么?