Class LogMasking

java.lang.Object
cloud.opencode.base.log.enhance.LogMasking

public final class LogMasking extends Object
Log Masking - Sensitive Data Masking Utility 日志脱敏 - 敏感数据脱敏工具

LogMasking provides utilities for masking sensitive data in log messages, such as passwords, phone numbers, and ID cards.

LogMasking 提供在日志消息中脱敏敏感数据的工具,如密码、手机号和身份证。

Example | 示例:

// Register masking rules
LogMasking.registerRule("password", MaskingStrategy.PASSWORD);
LogMasking.registerRule("phone", MaskingStrategy.PHONE);

// Mask values
String masked = LogMasking.mask("13812345678", MaskingStrategy.PHONE);
// Output: 138****5678

Features | 主要功能:

  • Built-in strategies for phone, email, ID card, bank card, password - 内置手机号、邮箱、身份证、银行卡、密码脱敏策略
  • Field-name-based masking rules - 基于字段名的脱敏规则
  • Pattern-based custom masking rules - 基于模式的自定义脱敏规则
  • @Mask annotation support - @Mask 注解支持

Security | 安全性:

  • Thread-safe: Yes (ConcurrentHashMap for rules) - 线程安全: 是(ConcurrentHashMap 存储规则)
  • Null-safe: Yes (returns null/empty as-is) - 空值安全: 是(null/空值原样返回)
Since:
JDK 25, opencode-base-log V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • registerRule

      public static void registerRule(String fieldName, LogMasking.MaskingStrategy strategy)
      Registers a masking rule for a field name. 为字段名注册脱敏规则。
      Parameters:
      fieldName - the field name pattern - 字段名模式
      strategy - the masking strategy - 脱敏策略
    • registerPatternRule

      public static void registerPatternRule(String pattern, Function<String,String> masker)
      Registers a pattern-based masking rule. 注册基于模式的脱敏规则。
      Parameters:
      pattern - the regex pattern - 正则模式
      masker - the masking function - 脱敏函数
    • clearRules

      public static void clearRules()
      Clears all masking rules. 清除所有脱敏规则。
    • mask

      public static String mask(String value, LogMasking.MaskingStrategy strategy)
      Masks a value using the specified strategy. 使用指定策略脱敏值。
      Parameters:
      value - the value to mask - 要脱敏的值
      strategy - the strategy - 策略
      Returns:
      the masked value - 脱敏后的值
    • maskByField

      public static String maskByField(String fieldName, String value)
      Masks a value based on the field name. 根据字段名脱敏值。
      Parameters:
      fieldName - the field name - 字段名
      value - the value - 值
      Returns:
      the masked value - 脱敏后的值
    • shouldMask

      public static boolean shouldMask(String fieldName)
      Checks if a field should be masked. 检查字段是否应该被脱敏。
      Parameters:
      fieldName - the field name - 字段名
      Returns:
      true if should be masked - 如果应该脱敏返回 true