第一步 设置脱敏过滤器
package com.datian.nongtian.common.core.filter;
import cn.hutool.core.util.DesensitizedUtil;
import cn.hutool.core.util.StrUtil;
import com.datian.nongtian.common.core.annotation.DesensitizationField;
import com.datian.nongtian.common.core.enums.DesensitizedType;
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.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.util.ObjectUtils;
import java.io.IOException;
import java.util.Objects;
/**
* 麻木过滤器 脱敏过滤器
*
*/
@AllArgsConstructor
@NoArgsConstructor
public final class DesensitizedFilter extends JsonSerializer<String> implements ContextualSerializer {
/**
* 脱敏类型
*/
private DesensitizedType type;
@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
if (ObjectUtils.isEmpty(value)) {
return;
}
String val = "";
switch (type) {
case NAME:
val = DesensitizedUtil.chineseName(value);
break;
case MOBILE:
val = DesensitizedUtil.mobilePhone(value);
break;
case EMAIL:
int index = StrUtil.indexOf(value, '@');
if (index <= 1) {
val = DesensitizedUtil.email(value);
break;
}
val = StrUtil.hide(value, 3, index);
break;
case PASSWORD:
val = DesensitizedUtil.password(value);
break;
default:
val = value;
break;
}
//修改数据
gen.writeString(val);
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property)
throws JsonMappingException {
if (ObjectUtils.isEmpty(property)) {
return prov.findNullValueSerializer(property);
}
if (!Objects.equals(property.getType().getRawClass(), String.class)) {
return prov.findNullValueSerializer(property);
}
//是否有注解
DesensitizationField annotation = property.getAnnotation(DesensitizationField.class);
if (ObjectUtils.isEmpty(annotation)) {
return prov.findNullValueSerializer(property);
}
//修改值
return new DesensitizedFilter(annotation.type());
}
}
第二步自定义注解
package com.datian.nongtian.common.core.annotation;
import com.datian.nongtian.common.core.enums.DesensitizedType;
import com.datian.nongtian.common.core.filter.DesensitizedFilter;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import java.lang.annotation.*;
/**
* 脱敏注解
*/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@JacksonAnnotationsInside
@JsonSerialize(using = DesensitizedFilter.class)
public @interface DesensitizationField {
/**
* 脱敏方式<br>
*
* <p>默认按名称脱敏
*/
DesensitizedType type() default DesensitizedType.NAME;
}
第三步设置类型
package com.datian.nongtian.common.core.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 脱敏类型
*
* @author zhouf
* @date 2023年9月22日15:25:24
*/
@Getter
@AllArgsConstructor
public enum DesensitizedType {
NAME("姓名"),
ID_CARD("身份证"),
MOBILE("联系方式"),
EMAIL("邮箱"),
PASSWORD("密文");
private final String name;
}
第四步实体类字段增加注解
@DesensitizationField(type = DesensitizedType.MOBILE)