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)
    • 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 | 验证结果
    • validate

      public ValidationResult validate(String id, String answer, String clientId, TrajectoryData trajectory)
      Validates with trajectory data for enhanced bot detection. 使用轨迹数据进行增强机器人检测验证。

      When trajectory data is provided, it is analyzed first. If the trajectory indicates bot-like behavior, the validation is rejected immediately.

      当提供轨迹数据时,首先进行轨迹分析。如果轨迹表明机器人行为,验证将立即被拒绝。

      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      answer - the provided answer | 提供的答案
      clientId - the client identifier | 客户端标识符
      trajectory - the trajectory data, may be null | 轨迹数据,可以为 null
      Returns:
      the validation result | 验证结果
      Since:
      JDK 25, opencode-base-captcha V1.0.3
    • validate

      public ValidationResult validate(String id, String answer, String clientId, TrajectoryData trajectory, boolean caseSensitive)
      Validates with trajectory data and case sensitivity option. 使用轨迹数据和大小写敏感选项进行验证。

      When trajectory data is provided, it is analyzed first. If the trajectory indicates bot-like behavior (any result other than HUMAN or INSUFFICIENT_DATA), the validation is rejected with a suspicious behavior result.

      当提供轨迹数据时,首先进行轨迹分析。如果轨迹表明机器人行为 (任何非 HUMANINSUFFICIENT_DATA 的结果), 验证将以可疑行为结果被拒绝。

      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      answer - the provided answer | 提供的答案
      clientId - the client identifier | 客户端标识符
      trajectory - the trajectory data, may be null | 轨迹数据,可以为 null
      caseSensitive - whether case sensitive | 是否区分大小写
      Returns:
      the validation result | 验证结果
      Since:
      JDK 25, opencode-base-captcha V1.0.3
    • getAnalyzer

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

      public TrajectoryAnalyzer getTrajectoryAnalyzer()
      Gets the trajectory analyzer. 获取轨迹分析器。
      Returns:
      the trajectory analyzer | 轨迹分析器
      Since:
      JDK 25, opencode-base-captcha V1.0.3
    • clearOldRecords

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