背景
敏感信息如手机号、身份证、邮箱等信息需要脱敏后展示给前台,如果需要查看,则需要申请权限,查询时需要记录操作日志。
方案
通过JsonSerializer和注解,在json序列化的时候做脱敏操作
此处使用redis存储了加密后的key和明文,如果不需要反查和记录操作日志可以去掉相关代码
流程
代码
脱敏工具类
package com.sky.ddtcode.desensitization;import cn.hutool.core.util.StrUtil;/*** 脱敏工具类*/
public class DesensitizedUtil
{/*** 密码的全部字符都用*代替,比如:******** @param password 密码* @return 脱敏后的密码*/public static String password(String password){if (StrUtil.isBlank(password)){return StrUtil.EMPTY;}return StrUtil.repeat('*', password.length());}/*** 车牌中间用*代替,如果是错误的车牌,不处理** @param carLicense 完整的车牌号* @return 脱敏后的车牌*/public static String carLicense(String carLicense){if (StrUtil.isBlank(carLicense)){return StrUtil.EMPTY;}// 普通车牌if (carLicense.length() == 7){carLicense = StrUtil.hide(carLicense, 3, 6);}else if (carLicense.length() == 8){// 新能源车牌carLicense = StrUtil.hide(carLicense, 3, 7);}return carLicense;}
}
脱敏策略枚举类
package com.sky.ddtcode.desensitization;import java.util.function.Function;/*** 脱敏策略,枚举类,针对不同的数据定制特定的策略*/
public enum SensitiveStrategy
{/*** 姓名,第2位星号替换*/USERNAME(s -> s.replaceAll("(\\S)\\S(\\S*)", "$1*$2")),/*** 密码,全部字符都用*代替*/PASSWORD(DesensitizedUtil::password),/*** 身份证,中间10位星号替换*/ID_CARD(s -> s.replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1** **** ****$2")),/*** 手机号,中间4位星号替换*/PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")),/*** 电子邮箱,仅显示第一个字母和@后面的地址显示,其他星号替换*/EMAIL(s -> s.replaceAll("(^.)[^@]*(@.*$)", "$1****$2")),/*** 银行卡号,保留最后4位,其他星号替换*/BANK_CARD(s -> s.replaceAll("\\d{15}(\\d{3})", "**** **** **** **** $1")),/*** 车牌号码,包含普通车辆、新能源车辆*/CAR_LICENSE(DesensitizedUtil::carLicense);private final Function<String, String> desensitizer;SensitiveStrategy(Function<String, String> desensitizer){this.desensitizer = desensitizer;}public Function<String, String> desensitizer(){return desensitizer;}
}
脱敏注解
package com.sky.ddtcode.desensitization;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;@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@JacksonAnnotationsInside
@JsonSerialize(using = SensitiveJsonSerializer.class)
public @interface Sensitive {//脱敏策略SensitiveStrategy strategy();
}
序列化注解自定义实现
此处的加密数据明文存储在redis里面,查询的时候根据rediskey获取数据,并记录操作日志。
此处是为了统一所以采用此种方式,也可以针对单个信息直接接口查询记录操作日志,但是接口会比较多
package com.sky.ddtcode.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 org.springframework.beans.factory.annotation.Autowired;import java.io.IOException;
import java.util.Objects;
import java.util.UUID;/*** 序列化注解自定义实现* JsonSerializer<String>:指定String 类型,serialize()方法用于将修改后的数据载入*/
public class SensitiveJsonSerializer extends JsonSerializer<String> implements ContextualSerializer {@AutowiredRedisService redisService;private SensitiveStrategy strategy;@Overridepublic void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {String newValue = strategy.desensitizer().apply(value);String guid=UUID.randomUUID().toString();String sensitiveValue=newValue+"_"+guid;redisService.save(guid,value);gen.writeString(sensitiveValue);//如果不需要反查明文信息,则可以去掉redisService直接使用下面的代码//gen.writeString(strategy.desensitizer().apply(value));}/*** 获取属性上的注解属性*/@Overridepublic JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {Sensitive annotation = property.getAnnotation(Sensitive.class);if (Objects.nonNull(annotation) && Objects.equals(String.class, property.getType().getRawClass())) {this.strategy = annotation.strategy();return this;}return prov.findValueSerializer(property.getType(), property);}
}
测试实体类
@Data
public class Person {/*** 真实姓名*/@Sensitive(strategy = SensitiveStrategy.USERNAME)private String realName;/*** 电话号码*/@Sensitive(strategy = SensitiveStrategy.PHONE)private String phoneNumber;/*** 身份证号码*/@Sensitive(strategy = SensitiveStrategy.ID_CARD)private String idCard;
}
测试类
package com.sky.ddtcode.controller;import com.sky.ddtcode.desensitization.Person;
import com.sky.ddtcode.desensitization.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/sensitive")
public class SensitiveController {@AutowiredRedisService redisService;@GetMapping("/test")public Person test(){Person user = new Person();user.setRealName("skywhite");user.setPhoneNumber("1888888888");user.setIdCard("411101199901013344");return user;}//如果不需要反查明文则可以不要这个接口和redisService服务@GetMapping("/realValue")public String realValue(String guid){String value= redisService.getValue(guid);//todo 记录操作日志,操作人信息,数据类型信息之类的return value;}
}