Class LogMasking
java.lang.Object
cloud.opencode.base.log.enhance.LogMasking
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interfaceAnnotation for marking fields that should be masked in logs.static enumMasking strategies for different data types. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidClears all masking rules.static Stringmask(String value, LogMasking.MaskingStrategy strategy) Masks a value using the specified strategy.static StringmaskByField(String fieldName, String value) Masks a value based on the field name.static voidregisterPatternRule(String pattern, Function<String, String> masker) Registers a pattern-based masking rule.static voidregisterRule(String fieldName, LogMasking.MaskingStrategy strategy) Registers a masking rule for a field name.static booleanshouldMask(String fieldName) Checks if a field should be masked.
-
Method Details
-
registerRule
Registers a masking rule for a field name. 为字段名注册脱敏规则。- Parameters:
fieldName- the field name pattern - 字段名模式strategy- the masking strategy - 脱敏策略
-
registerPatternRule
-
clearRules
public static void clearRules()Clears all masking rules. 清除所有脱敏规则。 -
mask
Masks a value using the specified strategy. 使用指定策略脱敏值。- Parameters:
value- the value to mask - 要脱敏的值strategy- the strategy - 策略- Returns:
- the masked value - 脱敏后的值
-
maskByField
-
shouldMask
Checks if a field should be masked. 检查字段是否应该被脱敏。- Parameters:
fieldName- the field name - 字段名- Returns:
- true if should be masked - 如果应该脱敏返回 true
-