1.自定义注解@DesensitizationEncrypt
package com.zkinginfo.utils.desensitization;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.zkinginfo.enums.DesensitizationEnum;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Author 王天佑
* email: 2102376305@qq.com
* Date: 2023/4/11
* Time: 9:37
*/
@Target(ElementType.FIELD) // 作用域
@Retention(RetentionPolicy.RUNTIME) //
@JacksonAnnotationsInside // 表示自定义自己的注解DesensitizationEncrypt
@JsonSerialize(using = DesensitizationSerializer.class) // 该注解使用序列化的方式
public @interface DesensitizationEncrypt {
/**
* 脱敏数据类型(没给默认值,所以使用时必须指定type)
*/
DesensitizationEnum type();
/**
* 前置不需要打码的长度
*/
int prefixNoMaskLen() default 1;
/**
* 后置不需要打码的长度
*/
int suffixNoMaskLen() default 1;
/**
* 用什么打码
*/
String symbol() default "*";
}
2.枚举类:DesensitizationEnum
package com.zkinginfo.enums;
import lombok.Getter;
/**
* Author 王天佑
* email: 2102376305@qq.com
* Date: 2023/4/11
* Time: 9:36
*/
@Getter
public enum DesensitizationEnum {
/** 自定义(此项需设置脱敏的范围)*/
CUSTOMER,
/** 姓名 */
NAME,
/** 身份证号 */
ID_CARD,
/** 手机号 */
PHONE,
/** 邮箱 */
EMAIL,
}
3.使用@JsonSerialize,DesensitizationSerializer
package com.zkinginfo.utils.desensitization;
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 com.zkinginfo.enums.DesensitizationEnum;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import java.util.Objects;
/**
* Author 王天佑
* email: 2102376305@qq.com
* Date: 2023/4/11
* Time: 9:39
*/
@NoArgsConstructor
@AllArgsConstructor
public class DesensitizationSerializer extends JsonSerializer<String> implements ContextualSerializer {
// 脱敏类型
private DesensitizationEnum desensitizationEnum;
// 前几位不脱敏
private Integer prefixNoMaskLen;
// 最后几位不脱敏
private Integer suffixNoMaskLen;
// 用什么打码
private String symbol;
@Override
public void serialize(final String origin, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException {
if (StringUtils.isNotBlank(origin) && null != desensitizationEnum) {
switch (desensitizationEnum) {
case CUSTOMER:
jsonGenerator.writeString(DesensitizationUtil.desValue(origin, prefixNoMaskLen, suffixNoMaskLen, symbol));
break;
case NAME:
jsonGenerator.writeString(DesensitizationUtil.hideChineseName(origin));
break;
case ID_CARD:
jsonGenerator.writeString(DesensitizationUtil.hideIDCard(origin));
break;
case PHONE:
jsonGenerator.writeString(DesensitizationUtil.hidePhone(origin));
break;
case EMAIL:
jsonGenerator.writeString(DesensitizationUtil.hideEmail(origin));
break;
default:
throw new IllegalArgumentException("unknown privacy type enum ");
}
} else {
jsonGenerator.writeString("");
}
}
@Override
public JsonSerializer<?> createContextual(final SerializerProvider serializerProvider, final BeanProperty beanProperty) throws JsonMappingException {
if (beanProperty != null) {
if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) {
DesensitizationEncrypt desensitizationEncrypt = beanProperty.getAnnotation(DesensitizationEncrypt.class);
if (desensitizationEncrypt == null) {
desensitizationEncrypt = beanProperty.getContextAnnotation(DesensitizationEncrypt.class);
}
if (desensitizationEncrypt != null) {
DesensitizationSerializer result = new DesensitizationSerializer(desensitizationEncrypt.type(), desensitizationEncrypt.prefixNoMaskLen(), desensitizationEncrypt.suffixNoMaskLen(), desensitizationEncrypt.symbol());
return result;
}
}
return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
}
return serializerProvider.findNullValueSerializer(null);
}
}
4.对象实体使用方法
/** 用户电话 */
@DesensitizationEncrypt(type = DesensitizationEnum.PHONE)
@Excel(name = "用户电话")
@ApiModelProperty( "用户电话")
private String custPhone;