美文网首页
在使用Mybatis-plus执行查询的时候,如何对关键字段进行

在使用Mybatis-plus执行查询的时候,如何对关键字段进行

作者: zhy_zhy | 来源:发表于2024-07-03 09:42 被阅读0次

为了在读取 用户 表中的 mobile 字段时进行脱敏处理,并实现一个通用的方法以便将来对其他字段例如:邮箱、身份证、姓名等进行脱敏处理,可以采用以下步骤:

1. 添加依赖

首先,在 pom.xml 文件中添加必要的依赖,包括 Spring Boot、MyBatis-Plus 和 Hutool 工具库:

<dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency>    <dependency>        <groupId>com.baomidou</groupId>        <artifactId>mybatis-plus-boot-starter</artifactId>        <version>3.5.2</version>    </dependency>    <dependency>        <groupId>cn.hutool</groupId>        <artifactId>hutool-all</artifactId>        <version>5.8.16</version>    </dependency></dependencies>

2. 创建脱敏注解

定义一个自定义注解 @Sensitive,用于标识需要脱敏的字段:

package com.example.demo.annotation;import java.lang.annotation.*;@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)@Documentedpublic @interface Sensitive {    SensitiveType type();}enum SensitiveType {    MOBILE,    EMAIL,    ID_CARD}

3. 创建脱敏拦截器

实现一个通用的脱敏拦截器 DesensitizationInterceptor,用于处理不同类型的脱敏逻辑:

package com.example.demo.handler;package com.central.db.sensitive;import cn.hutool.core.util.DesensitizedUtil;import lombok.extern.slf4j.Slf4j;import org.apache.ibatis.executor.resultset.ResultSetHandler;import org.apache.ibatis.plugin.Interceptor;import org.apache.ibatis.plugin.Intercepts;import org.apache.ibatis.plugin.Invocation;import org.apache.ibatis.plugin.Signature;import java.lang.reflect.Field;import java.sql.Statement;import java.util.List;@Slf4j@Intercepts({@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})})public class DesensitizationInterceptor implements Interceptor {    @Override    public Object intercept(Invocation invocation) throws Throwable {        Object result = invocation.proceed();        if (result instanceof List) {            for (Object obj : (List) result) {                desensitize(obj);            }        } else {            desensitize(result);        }        return result;    }    private void desensitize(Object obj) {        if (obj == null) {            return;        }                Field[] fields = obj.getClass().getDeclaredFields();        for (Field field : fields) {            if (field.isAnnotationPresent(Sensitive.class)) {                Sensitive sensitive = field.getAnnotation(Sensitive.class);                field.setAccessible(true);                try {                    String value = (String) field.get(obj);                    if (value != null) {                        field.set(obj, desensitizeValue(value, sensitive.type()));                    }                } catch (IllegalAccessException e) {                    e.printStackTrace();                }            }        }    }    private String desensitizeValue(String value, SensitiveType type) {        switch (type) {            case MOBILE:                return DesensitizedUtil.mobilePhone(value);            case EMAIL:                return DesensitizedUtil.email(value);            case ID_CARD:                return DesensitizedUtil.idCardNum(value, 1, 2);            default:                return value;        }    }}

4. 配置 MyBatis-Plus

在 MyBatis-Plus 配置中注册自定义的类型处理器:

package com.example.demo.config;import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;import com.example.demo.annotation.SensitiveType;import com.example.demo.handler.SensitiveTypeHandler;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class MyBatisPlusConfig {    @Bean    public Interceptor desensitizationInterceptor() {        return new DesensitizationInterceptor();    }        }

5. 修改实体类

Employee 实体类中使用自定义注解标识需要脱敏的字段:

package com.example.demo.entity;import com.baomidou.mybatisplus.annotation.TableId;import com.baomidou.mybatisplus.annotation.TableName;import com.example.demo.annotation.Sensitive;import com.example.demo.annotation.SensitiveType;import lombok.Data;@Data@TableName("employee")public class Employee {    @TableId    private Long id;    @Sensitive(type = SensitiveType.MOBILE)    private String mobile;    }

6. 测试

编写测试代码,验证脱敏功能是否正常工作:

package com.example.demo;import com.example.demo.entity.Employee;import com.example.demo.mapper.EmployeeMapper;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.CommandLineRunner;import org.springframework.stereotype.Component;@Componentpublic class TestRunner implements CommandLineRunner {    @Autowired    private EmployeeMapper employeeMapper;    @Override    public void run(String... args) throws Exception {        Employee employee = employeeMapper.selectById(1L);        System.out.println("脱敏后的手机号: " + employee.getMobile());    }}

通过以上步骤,你可以实现一个通用的数据脱敏方法,适用于不同字段和表的关键字段。这样可以有效保护敏感数据,确保数据隐私和安全。

相关文章

  • mybatis-plus 常用api

    Mybatis-Plus 只查询某些字段 select Update

  • mysql执行计划(explain)

    执行计划就是sql的执行查询的顺序,以及如何使用索引查询,返回的结果集的行数 重要字段:type,rows,key...

  • 联合索引

    索引 索引的使用 什么时候使用索引表的主关键字 表的字段唯一约束 直接条件查询的字段 查询中与其它表关联的字段 查...

  • Mybatis中LambdaQueryWrapper和Lambd

    使用mybatis-plus查询的时候经常会使用到QueryWrapper来构建查询条件,然而我们在写查询属性的时...

  • Mysql常见索引失效情况

    1.被索引字段发生隐式转换 Mysql执行器在执行sql查询的时候,会自动将与原字段类型不匹配的值进行类型转换 我...

  • elasticsearch bool查询

    在查询时,如果需要同时对多个字段进行多个查询,那么可以使用bool查询语句. bool查询接收如下参数: must...

  • MYSQL JSON值查询

    mysql根据json字段的内容检索查询数据 使用 字段->'$.json属性'进行查询条件 使用json_ext...

  • Mybatis-Plus select不列出全部字段

    mybatis-plus select查询语句默认是查全部字段,有两种方法可以指定要查询的字段 1、user表只需...

  • mybatis的">="、"<="、"like"

    延伸:mybatis-plus对字段operate_time时间进行比较[https://www.jianshu....

  • 数据分组

    group by关键字 作用:对查询结果进行分组处理 用法: 1.分组之后,不能将除分组字段之外的字段放在sele...

网友评论

      本文标题:在使用Mybatis-plus执行查询的时候,如何对关键字段进行

      本文链接:https://www.haomeiwen.com/subject/irubcjtx.html