Annotation Interface JsonMask


@Target(FIELD) @Retention(RUNTIME) @Documented public @interface JsonMask
JSON Mask - Data Masking for Sensitive Fields JSON 脱敏 - 敏感字段的数据脱敏

This annotation marks a field for data masking during serialization. It supports various masking strategies for sensitive data like passwords, phone numbers, ID cards, and email addresses.

此注解标记字段在序列化时进行数据脱敏。支持多种脱敏策略, 用于密码、手机号、身份证号和电子邮件等敏感数据。

Example | 示例:

public class User {
    @JsonMask(type = MaskType.PASSWORD)
    private String password;       // -> "******"

    @JsonMask(type = MaskType.PHONE)
    private String phone;          // "13812345678" -> "138****5678"

    @JsonMask(type = MaskType.ID_CARD)
    private String idCard;         // "110101199001011234" -> "110***********1234"

    @JsonMask(type = MaskType.EMAIL)
    private String email;          // "test@example.com" -> "t***@example.com"

    @JsonMask(type = MaskType.CUSTOM, pattern = "(?<=.{2}).(?=.{2})")
    private String customField;    // Custom regex masking
}

Features | 主要功能:

  • Multiple built-in masking strategies (phone, email, ID card, etc.) - 多种内置脱敏策略
  • Custom regex-based masking support - 自定义正则表达式脱敏支持
  • Configurable mask character and prefix/suffix lengths - 可配置脱敏字符和前缀/后缀长度

Security | 安全性:

  • Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
  • Null-safe: N/A - 空值安全: 不适用
Since:
JDK 25, opencode-base-json V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Mask type enumeration 脱敏类型枚举
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Whether masking is enabled (can be controlled at runtime).
    char
    Replacement character for masking.
    Custom regex pattern for CUSTOM mask type.
    int
    Number of visible characters at the start.
    int
    Number of visible characters at the end.
    The masking type.
  • Element Details

    • type

      The masking type. 脱敏类型。
      Returns:
      the mask type - 脱敏类型
      Default:
      FULL
    • pattern

      String pattern
      Custom regex pattern for CUSTOM mask type. 用于 CUSTOM 脱敏类型的自定义正则表达式。
      Returns:
      the pattern - 模式
      Default:
      ""
    • maskChar

      char maskChar
      Replacement character for masking. 脱敏替换字符。
      Returns:
      the mask character - 脱敏字符
      Default:
      '*'
    • prefixLength

      int prefixLength
      Number of visible characters at the start. 开头可见的字符数。
      Returns:
      the prefix length - 前缀长度
      Default:
      -1
    • suffixLength

      int suffixLength
      Number of visible characters at the end. 结尾可见的字符数。
      Returns:
      the suffix length - 后缀长度
      Default:
      -1
    • enabled

      boolean enabled
      Whether masking is enabled (can be controlled at runtime). 是否启用脱敏(可在运行时控制)。
      Returns:
      true if masking is enabled - 如果启用脱敏则返回 true
      Default:
      true