@Test public void testProcess() { DataMaskJsonFilter filter = new DataMaskJsonFilter(); TestUserMapingData test1 = new TestUserMapingData(); test1.setTel("13590908322"); test1.setTest("test"); test1.setStrArr(new String[]{"test1", "test2"}); ArrayList<String> strList = new ArrayList<>(); strList.add("test1"); strList.add("test2"); test1.setStrList(strList); Set<String> setStr = new HashSet<>(); setStr.add("test1"); test1.setSet(setStr); String json = JSON.toJSONString(test1, filter); test1 = JSON.parseObject(json, TestUserMapingData.class); assertThat(test1.getTel()).isEqualTo("135*****322"); assertThat(test1.getTest()).isEqualTo("test"); assertThat(test1.getStrArr()).contains("t****"); assertThat(test1.getStrList()).contains("t****"); assertThat(test1.getSet()).contains("t****"); TestData test2 = new TestData(); test2.setName("name"); test2.setPhone("13655555555"); test2.setAccount("苹果"); json = JSON.toJSONString(test2, filter); test2 = JSON.parseObject(json, TestData.class); assertThat(test2.getName()).isEqualTo("**me"); assertThat(test2.getPhone()).isEqualTo("13*******55"); assertThat(test2.getAccount()).isEqualTo("苹*"); }
@Override public Object process(BeanContext context, Object object, String name, Object value) { if (null == value) { return null; } try { Field field = context.getField(); if (field.getType() == String.class) { if (((String) value).length() == 0) { return value; } Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); return mask((String) value, sensitive, sensitiveType); } else if (field.getType() == String[].class) { Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); if (sensitiveType == null) { return value; } String[] strArr = (String[]) value; for (int i = 0; i < strArr.length; i++) { strArr[i] = mask(strArr[i], sensitive, sensitiveType); } return strArr; } else if (Collection.class.isAssignableFrom(field.getType())) { Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); if (sensitiveType == null) { return value; } if(!isStringCollection(field)){ return value; } Collection<String> newValue = (Collection<String>) value.getClass().newInstance(); for (String item : (Collection<String>) value) { newValue.add(mask(item, sensitive, sensitiveType)); } return newValue; } else { return value; } } catch (Exception e) { return value; } }
DataMaskJsonFilter implements ContextValueFilter { @Override public Object process(BeanContext context, Object object, String name, Object value) { if (null == value) { return null; } try { Field field = context.getField(); if (field.getType() == String.class) { if (((String) value).length() == 0) { return value; } Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); return mask((String) value, sensitive, sensitiveType); } else if (field.getType() == String[].class) { Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); if (sensitiveType == null) { return value; } String[] strArr = (String[]) value; for (int i = 0; i < strArr.length; i++) { strArr[i] = mask(strArr[i], sensitive, sensitiveType); } return strArr; } else if (Collection.class.isAssignableFrom(field.getType())) { Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); if (sensitiveType == null) { return value; } if(!isStringCollection(field)){ return value; } Collection<String> newValue = (Collection<String>) value.getClass().newInstance(); for (String item : (Collection<String>) value) { newValue.add(mask(item, sensitive, sensitiveType)); } return newValue; } else { return value; } } catch (Exception e) { return value; } } }
DataMaskJsonFilter implements ContextValueFilter { @Override public Object process(BeanContext context, Object object, String name, Object value) { if (null == value) { return null; } try { Field field = context.getField(); if (field.getType() == String.class) { if (((String) value).length() == 0) { return value; } Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); return mask((String) value, sensitive, sensitiveType); } else if (field.getType() == String[].class) { Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); if (sensitiveType == null) { return value; } String[] strArr = (String[]) value; for (int i = 0; i < strArr.length; i++) { strArr[i] = mask(strArr[i], sensitive, sensitiveType); } return strArr; } else if (Collection.class.isAssignableFrom(field.getType())) { Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); if (sensitiveType == null) { return value; } if(!isStringCollection(field)){ return value; } Collection<String> newValue = (Collection<String>) value.getClass().newInstance(); for (String item : (Collection<String>) value) { newValue.add(mask(item, sensitive, sensitiveType)); } return newValue; } else { return value; } } catch (Exception e) { return value; } }  }
DataMaskJsonFilter implements ContextValueFilter { @Override public Object process(BeanContext context, Object object, String name, Object value) { if (null == value) { return null; } try { Field field = context.getField(); if (field.getType() == String.class) { if (((String) value).length() == 0) { return value; } Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); return mask((String) value, sensitive, sensitiveType); } else if (field.getType() == String[].class) { Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); if (sensitiveType == null) { return value; } String[] strArr = (String[]) value; for (int i = 0; i < strArr.length; i++) { strArr[i] = mask(strArr[i], sensitive, sensitiveType); } return strArr; } else if (Collection.class.isAssignableFrom(field.getType())) { Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); if (sensitiveType == null) { return value; } if(!isStringCollection(field)){ return value; } Collection<String> newValue = (Collection<String>) value.getClass().newInstance(); for (String item : (Collection<String>) value) { newValue.add(mask(item, sensitive, sensitiveType)); } return newValue; } else { return value; } } catch (Exception e) { return value; } }  @Override Object process(BeanContext context, Object object, String name, Object value); }
DataMaskJsonFilter implements ContextValueFilter { @Override public Object process(BeanContext context, Object object, String name, Object value) { if (null == value) { return null; } try { Field field = context.getField(); if (field.getType() == String.class) { if (((String) value).length() == 0) { return value; } Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); return mask((String) value, sensitive, sensitiveType); } else if (field.getType() == String[].class) { Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); if (sensitiveType == null) { return value; } String[] strArr = (String[]) value; for (int i = 0; i < strArr.length; i++) { strArr[i] = mask(strArr[i], sensitive, sensitiveType); } return strArr; } else if (Collection.class.isAssignableFrom(field.getType())) { Sensitive sensitive = field.getAnnotation(Sensitive.class); SensitiveType sensitiveType = getSensitiveType(field, sensitive); if (sensitiveType == null) { return value; } if(!isStringCollection(field)){ return value; } Collection<String> newValue = (Collection<String>) value.getClass().newInstance(); for (String item : (Collection<String>) value) { newValue.add(mask(item, sensitive, sensitiveType)); } return newValue; } else { return value; } } catch (Exception e) { return value; } }  @Override Object process(BeanContext context, Object object, String name, Object value);  }