Class BehaviorCaptchaValidator

java.lang.Object
cloud.opencode.base.captcha.validator.BehaviorCaptchaValidator
All Implemented Interfaces:
CaptchaValidator

public final class BehaviorCaptchaValidator extends Object implements CaptchaValidator
Behavior Captcha Validator - Validation with behavior analysis 行为验证码验证器 - 带行为分析的验证

This validator combines answer validation with behavior analysis to detect automated attacks and suspicious patterns.

此验证器结合答案验证和行为分析来检测自动攻击和可疑模式。

Features | 主要功能:

  • Response time analysis - 响应时间分析
  • Failure rate tracking - 失败率跟踪
  • Consistent timing detection - 一致时间检测
  • Bot behavior identification - 机器人行为识别

Usage Example | 使用示例:

CaptchaStore store = CaptchaStore.memory();
BehaviorCaptchaValidator validator = new BehaviorCaptchaValidator(store);

// Record creation time
validator.recordCreation(captchaId, clientId);
// (validation continues...)

// Later, validate with behavior check
ValidationResult result = validator.validate(captchaId, answer, clientId);
if (result.code() == ResultCode.SUSPICIOUS_BEHAVIOR) {
    // Handle bot detection
}

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap) - 线程安全: 是(使用ConcurrentHashMap)
  • Null-safe: No (store, id, and answer must not be null) - 空值安全: 否(存储、ID和答案不能为null)
Since:
JDK 25, opencode-base-captcha V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • BehaviorCaptchaValidator

      public BehaviorCaptchaValidator(CaptchaStore store)
      Creates a new behavior validator with the specified store. 使用指定存储创建新行为验证器。
      Parameters:
      store - the CAPTCHA store | 验证码存储
    • BehaviorCaptchaValidator

      public BehaviorCaptchaValidator(CaptchaStore store, BehaviorAnalyzer analyzer)
      Creates a new behavior validator with custom analyzer. 使用自定义分析器创建新行为验证器。
      Parameters:
      store - the CAPTCHA store | 验证码存储
      analyzer - the behavior analyzer | 行为分析器
  • Method Details

    • recordCreation

      public void recordCreation(String captchaId, String clientId)
      Records CAPTCHA creation for a client. 为客户端记录验证码创建。
      Parameters:
      captchaId - the CAPTCHA ID | 验证码 ID
      clientId - the client identifier | 客户端标识符
    • validate

      public ValidationResult validate(String id, String answer)
      Description copied from interface: CaptchaValidator
      Validates a CAPTCHA answer. 验证验证码答案。
      Specified by:
      validate in interface CaptchaValidator
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      answer - the provided answer | 提供的答案
      Returns:
      the validation result | 验证结果
    • validate

      public ValidationResult validate(String id, String answer, boolean caseSensitive)
      Description copied from interface: CaptchaValidator
      Validates a CAPTCHA answer with case sensitivity option. 验证验证码答案(带大小写敏感选项)。
      Specified by:
      validate in interface CaptchaValidator
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      answer - the provided answer | 提供的答案
      caseSensitive - whether case sensitive | 是否区分大小写
      Returns:
      the validation result | 验证结果
    • validate

      public ValidationResult validate(String id, String answer, String clientId)
      Validates with explicit client ID. 使用明确的客户端 ID 进行验证。
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      answer - the provided answer | 提供的答案
      clientId - the client identifier | 客户端标识符
      Returns:
      the validation result | 验证结果
    • validate

      public ValidationResult validate(String id, String answer, String clientId, boolean caseSensitive)
      Validates with explicit client ID and case sensitivity. 使用明确的客户端 ID 和大小写敏感选项进行验证。
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      answer - the provided answer | 提供的答案
      clientId - the client identifier | 客户端标识符
      caseSensitive - whether case sensitive | 是否区分大小写
      Returns:
      the validation result | 验证结果
    • getAnalyzer

      public BehaviorAnalyzer getAnalyzer()
      Gets the behavior analyzer. 获取行为分析器。
      Returns:
      the behavior analyzer | 行为分析器
    • clearOldRecords

      public void clearOldRecords()
      Clears old creation records. 清除旧的创建记录。