自定义注解实现数据脱敏,国密加解密(sm4)

自定义注解实现数据脱敏,,国密加解密(sm4)
在实际开发中经常会遇到有一些信息不能全部展示用户,需要隐藏(可以叫脱敏)一部分的情况比如地址,电话,手机号,身份证等。
脱敏的做法目前我知道的方法有:
1):业务代码脱敏:顾名思义就是拿到数据需要脱敏的那个字段进过一系列的脱敏规则替换成自己想要的格式。
2):自定义注解+aop切面的方式去完成字段脱敏的目的。
3):自定义注解+序列化的方式对数据进行脱敏。
其中效率最慢的当属于代码业务代码方式,最优的方式当属于自定义注解的方式,减少代码量,提高工作效率,需要脱敏的字段仅需要加一个注解就可以达到目的。
今天就记录下第三种方式,自定义注解+序列化的方式。
首先先定义一个需要脱敏的策略


/**
 * @description: 脱敏策略
 * @author: 
 * @date: 2023/6/18 19:45
 * @Version: 1.0
 */
public enum DesensitizationEnum {

    /**
     * 名称脱敏
     */
    USERNAME(s -> s.replaceAll("(\\S)\\S(\\S*)", "$1*$2"))
    ,
    /**
     * 手机号脱敏
     */
    MOBILE_PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"))
    ,
    /**
     *地址脱敏
     */
    ADDRESS(s -> s.replaceAll("(\\S{3})\\S{2}(\\S*)\\S{2}", "$1****$2****"))
    ;

    /**
     * 成员变量  是一个接口类型
     */
    private Function<String, String> function;

    DesensitizationEnum(Function<String, String> function) {
        this.function = function;
    }

    public Function<String, String> function() {
        return this.function;
    }
}

然后自定义一个注解

import com.chaozhou.test01.dingShiRenWu.Annontion.Aspect.DesensitizationEnum;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @description:
 * @author:
 * @date: 2023/6/18 17:23
 * @Version: 1.0
 */

@Target({ElementType.FIELD}) //表明作用在字段上
@Retention(RetentionPolicy.RUNTIME) //什么情况下生效
@JacksonAnnotationsInside //标明序列化
@JsonSerialize(using = DesensitizationDataSerialize.class)
public @interface DesensitizationData {

    DesensitizationEnum function();
}

然后自定义一个序列化器

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.util.Objects;

/**
 * @description: 数据脱敏序列化器
 * @author: 
 * @date: 2023/6/18 19:17
 * @Version: 1.0
 */
@NoArgsConstructor
public class DesensitizationDataSerialize extends JsonSerializer<String> implements ContextualSerializer {

    private DesensitizationEnum fuincation;

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
        DesensitizationData annotation = beanProperty.getAnnotation(DesensitizationData.class);
        if(Objects.nonNull(annotation) && Objects.equals(beanProperty.getType().getRawClass(),String.class)){
            this.fuincation = annotation.function();
            return this;
        }
        return serializerProvider.findValueSerializer(beanProperty.getType(),beanProperty);
    }

    @Override
    public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeString(fuincation.function().apply(s));
    }

}

这样一个自定义脱敏注解就完成了。
使用方法:
只需要在返回参数需要脱敏的字段上加上响应注解就可以了

public class User implements Serializable {

    @TableId("id")
    private Integer id;

    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
    @ApiModelProperty(value = "出生年月")
    private Date birthday;

    @ApiModelProperty(value = "性别")
    private String gender;

    @ApiModelProperty(value = "姓名")
    private String username;

    @ApiModelProperty(value = "密码")
    private String password;

    @ApiModelProperty(value = "备注")
    private String remark;

    @ApiModelProperty(value = "状态")
    private String station;

    @ApiModelProperty(value = "联系电话")
    //手机号脱敏
    @DesensitizationData(function = DesensitizationEnum.MOBILE_PHONE)
    private String telephone;

}

结果
在这里插入图片描述
手机号已经脱敏成功,该方法单条,多条数据都支持。

国密加解密
需要用到的依赖,可以直接用hutool整合的框架实现

<dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.16</version>
        </dependency>
        <!--以下依赖不加亲测也可以实现-->
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.60</version>
        </dependency>

在以上方法中的策略中加入加解密规则
在这里插入图片描述
然后在需要加密或解密的字段中加入该注解,并注解标注正确的策略方式
在这里插入图片描述
修改一下上面的序列化器,防止需要需要解密的字段值我空的时候报“Null input buffer”的错
在这里插入图片描述
列表字段加密前
在这里插入图片描述

列表加密后
在这里插入图片描述

工具准备的好,代码效率高了不少。

猜你喜欢

转载自blog.csdn.net/weixin_51114236/article/details/131276402