基于序列化怎么实现jackson
这篇文章主要介绍“基于序列化怎么实现jackson”,在日常操作中,相信很多人在基于序列化怎么实现jackson问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”基于序列化怎么实现jackson”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
站在用户的角度思考问题,与客户深入沟通,找到滨湖网站设计与滨湖网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计、网站制作、企业官网、英文网站、手机端网站、网站推广、主机域名、网站空间、企业邮箱。业务覆盖滨湖地区。
基于序列化的实现
Fastjson实现
实现思路:
自定义注解,可让用户自定义脱敏方式,用于实体类的属性
基于ValueFilter进行 属性注解拦截,并多value进行替换脱敏
使用json序列化对象是指定自定义序列化Filter
核心代码如下
自定义注解Desensitization
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) public @interface Desensitization { /** * 脱敏规则 * * @return */ DesensitionEnum desensitionEnum(); }
脱敏方法
/** * 脱敏的函数接口 */ public interface Desensitizer extends Function{ } @Getter public enum DesensitionEnum { /** * 用户名脱敏 */ USERNAME("userName", "用户名",s -> s.replaceAll("(\\S)\\S(\\S*)", "$1*$2")), /** * 身份证号码脱敏 */ ID_CARD("idCard", "15或者18身份证号",s -> s.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1****$2")), /** * 手机号脱敏 */ PHONE("phone", "手机号",s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")), /** * 地址脱敏 */ ADDRESS("address", "地址脱敏",s -> s.replaceAll("(\\S{8})\\S{4}(\\S*)\\S{4}", "$1****$2****")); String fieldName; String fieldDescribe; private final Desensitizer desensitizer; DesensitionEnum(String fieldName, String fieldDescribe, Desensitizer desensitizer) { this.fieldName = fieldName; this.fieldDescribe = fieldDescribe; this.desensitizer = desensitizer; } }
具体拦截
public class FastjsonDesensitizeFilter implements ValueFilter { @Override public Object process(Object object, String name, Object value) { if (null == value || !(value instanceof String) || ((String) value).length() == 0) { return value; } try { Field field = object.getClass().getDeclaredField(name); Desensitization desensitization; if (String.class != field.getType() || (desensitization = field.getAnnotation(Desensitization.class)) == null) { return value; } return desensitization.desensitionEnum().getDesensitizer().apply((String) value); } catch (Exception e) { return value; } } }
Jackson实现
解释:
获取对象属性上的注解,根据属性得到相应的脱敏规则类型
按照规则类型进行value替换
自定义注解
@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @JsonSerialize(using = JacksonDesensitize.class) @JacksonAnnotationsInside public @interface Desensitization { /** * 脱敏规则 * * @return */ DesensitionEnum desensitionEnum(); } package com.demo.desensitization; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; 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 java.io.IOException; import java.util.Objects; public class JacksonDesensitize extends JsonSerializerimplements ContextualSerializer { private DesensitionEnum desensitionEnum; @Override public void serialize(String value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeString(desensitionEnum.getDesensitizer().apply((String) value)); } @Override public JsonSerializer> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException { if (beanProperty != null) { // 非 String 直接跳过 if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) { // 获取注解信息 Desensitization annotation = beanProperty.getAnnotation(Desensitization.class); if (annotation == null) { annotation = beanProperty.getContextAnnotation(Desensitization.class); } if (annotation != null) { // 获得注解上的值并赋值 this.desensitionEnum = annotation.desensitionEnum(); return this; } } return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty); } return serializerProvider.findNullValueSerializer(null); } }
执行结果
package com.demo.desensitization; import lombok.Data; import java.io.Serializable; @Data public class UserDTO implements Serializable { @Desensitization(desensitionEnum=DesensitionEnum.ID_CARD) private String idCard; @Desensitization(desensitionEnum=DesensitionEnum.USERNAME) private String username; @Desensitization(desensitionEnum=DesensitionEnum.PHONE) private String phone; @Desensitization(desensitionEnum=DesensitionEnum.ADDRESS) private String address; } public static void testFast(){ FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat, SerializerFeature.DisableCircularReferenceDetect); fastJsonConfig.setSerializeFilters(new FastjsonDesensitizeFilter()); UserDTO userDTO=new UserDTO(); userDTO.setUsername("张三"); userDTO.setPhone("15669057552"); userDTO.setIdCard("370826198901133299"); userDTO.setAddress("浙江省杭州市江干区火车东站"); String s = JSON.toJSONString(userDTO,new FastjsonDesensitizeFilter()); System.out.println(s); } public static void testJackson() throws JsonProcessingException { UserDTO userDTO=new UserDTO(); userDTO.setUsername("张三"); userDTO.setPhone("15669057552"); userDTO.setIdCard("370826198901133299"); userDTO.setAddress("浙江省杭州市江干区火车东站"); ObjectMapper objectMapper = new ObjectMapper(); String value = objectMapper.writeValueAsString(userDTO); System.out.println(value); }
最终输出结果
{"idCard":"3708****3299","username":"张*","phone":"156****7552","address":"浙江省杭州市江干区火车东站"}
到此,关于“基于序列化怎么实现jackson”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
文章标题:基于序列化怎么实现jackson
URL分享:http://ybzwz.com/article/jdjipi.html