1.首先创建注解类
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.jian.common.desen.BaseRule;
import com.jian.common.desen.DefaultRule;
import com.jian.common.desen.MyJsonSerializer;
import java.lang.annotation.*;
/**
* @author Administrator
*/
@JacksonAnnotationsInside
@JsonSerialize(using = MyJsonSerializer.class)
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface CustomSerializer {
/**
* 脱敏规则处理类
* @return
*/
Class<? extends BaseRule> value() default DefaultRule.class;
/**
* 正则,pattern和format必需同时有值。如果都有值时,优先使用正则进行规则替换
* @return
*/
String pattern() default "";
String format() default "";
}
2.创建注解检查 转换类
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.jian.common.annotation.CustomSerializer;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
@Slf4j
public class MyJsonSerializer extends JsonSerializer<String> implements ContextualSerializer {
/**
* 脱敏规则
*/
private BaseRule rule;
@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(rule.apply(value));
}
@Override
public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
//获取对象属性上的自定义注解
CustomSerializer customSerializer = property.getAnnotation(CustomSerializer.class);
if (null != customSerializer) {
try {
//根据注解的配置信息,创建对应脱敏规则处理类
this.rule = customSerializer.value().newInstance();
//如果正则信息不为空,则使用注解上的正则初始化到对应的脱敏规则处理类中
if (isNotBlank(customSerializer.pattern()) && isNotBlank(customSerializer.format())) {
this.rule.setRule(new RuleItem()
.setRegex(customSerializer.pattern())
.setFormat(customSerializer.format()));
}
return this;
} catch (Exception e) {
log.error("json转换处理异常", e);
}
}
return prov.findValueSerializer(property.getType(), property);
}
private boolean isNotBlank(String str) {
return null != str && str.trim().length() > 0;
}
}
3.创建数据脱敏基类
package com.jian.common.desen;
import lombok.Data;
import java.util.function.Function;
//脱敏处理基类
@Data
public abstract class BaseRule implements Function<String, String> {
/**
* 脱敏规则对象
*/
private RuleItem rule;
@Override
public String apply(String str) {
if (null == str) {
return null;
}
//初始化脱敏规则
initRule();
if (null == rule || null == rule.getRegex() || null == rule.getFormat()) {
return str;
}
//正则替换
return str.replaceAll(rule.getRegex(), rule.getFormat());
}
abstract void initRule();
}
4.注解参数以及默认处理类
import lombok.Data;
import lombok.experimental.Accessors;
@Data
@Accessors(chain = true)
public class RuleItem {
/**
* 正则
*/
private String regex;
/**
* 格式化显示
*/
private String format;
}
public class DefaultRule extends BaseRule {
@Override
void initRule() {
}
}
5.实现基类,自定义处理脱敏规则
例如手机号脱敏
//手机号脱敏处理类
public class PhoneRule extends BaseRule {
/**
* 仅显示前3位和后4位
*/
@Override
void initRule() {
setRule(new RuleItem()
.setRegex("(\\d{3})\\d*(\\d{4})")
.setFormat("$1****$2"));
}
}
身份证号码脱敏
//身份证号脱敏处理类
public class IdCardRule extends BaseRule {
/**
* 仅显示前6位和后4位
*/
@Override
void initRule() {
setRule(new RuleItem()
.setRegex("(\\d{6})\\d*(\\w{4})")
.setFormat("$1********$2"));
}
}
用户名脱敏
//姓名脱敏处理类
public class UserNameRule extends BaseRule {
/**
* 仅显示最后一个汉字
*/
@Override
void initRule() {
setRule(new RuleItem()
.setRegex("\\S*(\\S)")
.setFormat("**$1"));
}
}
6.实体类属性上面加上注解
@CustomSerializer(value = PhoneRule.class)
private String phonenumber;
这样子返回的数据,就会脱敏返回啦