Class CaptchaDifficultyAdapter

java.lang.Object
cloud.opencode.base.captcha.support.CaptchaDifficultyAdapter

public final class CaptchaDifficultyAdapter extends Object
Captcha Difficulty Adapter - Adaptive difficulty adjustment 验证码难度适配器 - 自适应难度调整

This class dynamically adjusts CAPTCHA difficulty based on client behavior, increasing difficulty for suspicious patterns and reducing for legitimate users.

此类根据客户端行为动态调整验证码难度,对可疑模式增加难度,对合法用户降低难度。

Features | 主要功能:

  • Failure rate based adaptation - 基于失败率的适应
  • Time based adjustment - 基于时间的调整
  • Risk level integration - 风险级别集成

Usage Examples | 使用示例:

CaptchaDifficultyAdapter adapter = new CaptchaDifficultyAdapter();

// Get adapted strength for client
CaptchaStrength strength = adapter.getStrength(clientId);

// Generate CAPTCHA with adapted difficulty
Captcha captcha = OpenCaptcha.generate(strength.toConfig());

// Record validation result
adapter.recordAttempt(clientId, success);

Security | 安全性:

  • Thread-safe: Yes (uses ConcurrentHashMap and ReentrantLock) - 线程安全: 是(使用ConcurrentHashMap和ReentrantLock)
  • Null-safe: No (clientId 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

    • CaptchaDifficultyAdapter

      public CaptchaDifficultyAdapter()
  • Method Details

    • getStrength

      public CaptchaStrength getStrength(String clientId)
      Gets the recommended strength for a client. 获取客户端的推荐强度。
      Parameters:
      clientId - the client identifier | 客户端标识符
      Returns:
      the recommended strength | 推荐强度
    • getConfig

      public CaptchaConfig getConfig(String clientId)
      Gets the adapted configuration for a client. 获取客户端的适应配置。
      Parameters:
      clientId - the client identifier | 客户端标识符
      Returns:
      the adapted configuration | 适应配置
    • getConfig

      public CaptchaConfig getConfig(String clientId, CaptchaConfig baseConfig)
      Gets the adapted configuration with custom base config. 使用自定义基础配置获取适应配置。
      Parameters:
      clientId - the client identifier | 客户端标识符
      baseConfig - the base configuration | 基础配置
      Returns:
      the adapted configuration | 适应配置
    • recordAttempt

      public void recordAttempt(String clientId, boolean success)
      Records a validation attempt. 记录一次验证尝试。
      Parameters:
      clientId - the client identifier | 客户端标识符
      success - whether the attempt was successful | 尝试是否成功
    • reset

      public void reset(String clientId)
      Resets the difficulty for a client. 重置客户端的难度。
      Parameters:
      clientId - the client identifier | 客户端标识符
    • getGlobalFailureRate

      public double getGlobalFailureRate()
      Gets the global failure rate. 获取全局失败率。
      Returns:
      the failure rate (0.0 - 1.0) | 失败率
    • getFailureCount

      public int getFailureCount(String clientId)
      Gets the failure count for a client. 获取客户端的失败次数。
      Parameters:
      clientId - the client identifier | 客户端标识符
      Returns:
      the failure count | 失败次数
    • getTrackedClientCount

      public int getTrackedClientCount()
      Gets the number of tracked clients. 获取跟踪的客户端数量。
      Returns:
      the number of clients | 客户端数量
    • clearAll

      public void clearAll()
      Clears all client records. 清除所有客户端记录。