Location>code7788 >text

Custom annotations for data desensitization

Popularity:135 ℃/2024-11-15 15:35:35

preamble

Some times, we may output some fields to do special processing in the output to the front-end, such as: ID number, phone and other information, in the front-end display when we need to desensitize the processing, this time through the custom annotations will be very useful. In Jackson to customize the annotations, we can through the@JacksonAnnotationsInsideannotation to implement, as shown in the following example:

I. Customizing annotations

import ;
import ;
import ;
import ;
import ;
import ;

@Target()
@Retention()
@JacksonAnnotationsInside
@JsonSerialize(using = SensitiveSerializer.class)
public @interface Sensitive {

    //encryption start position
    int start()default 0 ;

    //encryption end position
    int end() default 0 ;

    //encryption mask
    String mask() default "*" ;
}

Second, custom serialization processor SensitiveSerializer

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**
 * @author songwp
 * @date 2024-11-15
 * @desc custom serializer for desensitizing sensitive fields
*/
public class SensitiveSerializer extends JsonSerializer<String> implements ContextualSerializer {

    private Sensitive sensitive;

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        String val = value;
        if (sensitive != null && (val)) {
            String m = ();
            int start = ();
            int end = ();
            int totalLength = ();
            if (totalLength <= 2) {
                val = totalLength == 1 ? value + m : (0, 1) + m;
            } else if (totalLength <= 6) {
                val = (0, 1) + ("", (totalLength - 2, m)) + (totalLength - 1);
            } else {
                int prefixLength = (start, totalLength - 1);
                int suffixLength = (end, totalLength - 1);
                if (prefixLength > totalLength) {
                    prefixLength = totalLength / 2;
                }
                if (suffixLength > totalLength) {
                    suffixLength = totalLength / 2;
                }
                int maskLength = (0, totalLength - (prefixLength + suffixLength));
                if (maskLength == 0) {
                    prefixLength -= 2;
                    suffixLength -= 2;
                    maskLength = (2, totalLength - (prefixLength + suffixLength));
                }
                prefixLength = (prefixLength, totalLength - 1);
                suffixLength = (suffixLength, totalLength - 1);
                maskLength = totalLength - prefixLength - suffixLength;
                val = (0, prefixLength) + ("", (maskLength, m)) + (totalLength - suffixLength);
            }
        }
        (val);
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider prov, BeanProperty property) throws JsonMappingException {
        sensitive = (Sensitive.class);
        return this;
    }
}

Third, use the above annotations in the output Java bean

import ;
import ;
import ;
import ;
import ;
import ;
import ;

/**
 * @author songwp
 * @version 1.0
 * @date 2024-11-15
 * @description: user domain
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;
    @Sensitive(start = 2, end = 4)
    private String name;
    @Sensitive(start = 6, end = 4)
    private String idCard;
    @Sensitive(start = 4, end = 3)
    private String phone;
}

IV. The results are displayed on the front end as follows:

Sensitive data is desensitized.