1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| public interface DataMaskingOperation {
String MASK_CHAR = "*"; String mask(String content, String maskChar);
}
public enum DataMaskingFunc {
NO_MASK((str, maskChar) -> { return str; }), ALL_MASK((str, maskChar) -> { if (StringUtils.hasLength(str)) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.length(); i++) { sb.append(StringUtils.hasLength(maskChar) ? maskChar : DataMaskingOperation.MASK_CHAR); } return sb.toString(); } else { return str; } });
private final DataMaskingOperation operation;
private DataMaskingFunc(DataMaskingOperation operation) { this.operation = operation; }
public DataMaskingOperation operation() { return this.operation; }
}
public final class DataMaskingSerializer extends StdScalarSerializer<Object> { private final DataMaskingOperation operation;
public DataMaskingSerializer() { super(String.class, false); this.operation = null; }
public DataMaskingSerializer(DataMaskingOperation operation) { super(String.class, false); this.operation = operation; }
public boolean isEmpty(SerializerProvider prov, Object value) { String str = (String)value; return str.isEmpty(); }
public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException { if (Objects.isNull(operation)) { String content = DataMaskingFunc.ALL_MASK.operation().mask((String) value, null); gen.writeString(content); } else { String content = operation.mask((String) value, null); gen.writeString(content); } }
public final void serializeWithType(Object value, JsonGenerator gen, SerializerProvider provider, TypeSerializer typeSer) throws IOException { this.serialize(value, gen, provider); }
public JsonNode getSchema(SerializerProvider provider, Type typeHint) { return this.createSchemaNode("string", true); }
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException { this.visitStringFormat(visitor, typeHint); } }
|