Class BehaviorCaptchaValidator
java.lang.Object
cloud.opencode.base.captcha.validator.BehaviorCaptchaValidator
- All Implemented Interfaces:
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 Summary
ConstructorsConstructorDescriptionCreates a new behavior validator with the specified store.BehaviorCaptchaValidator(CaptchaStore store, BehaviorAnalyzer analyzer) Creates a new behavior validator with custom analyzer. -
Method Summary
Modifier and TypeMethodDescriptionvoidClears old creation records.Gets the behavior analyzer.Gets the trajectory analyzer.voidrecordCreation(String captchaId, String clientId) Validates a CAPTCHA answer.Validates a CAPTCHA answer with case sensitivity option.Validates with explicit client ID.Validates with explicit client ID and case sensitivity.validate(String id, String answer, String clientId, TrajectoryData trajectory) Validates with trajectory data for enhanced bot detection.validate(String id, String answer, String clientId, TrajectoryData trajectory, boolean caseSensitive) Validates with trajectory data and case sensitivity option.
-
Constructor Details
-
BehaviorCaptchaValidator
Creates a new behavior validator with the specified store. 使用指定存储创建新行为验证器。- Parameters:
store- the CAPTCHA store | 验证码存储
-
BehaviorCaptchaValidator
Creates a new behavior validator with custom analyzer. 使用自定义分析器创建新行为验证器。- Parameters:
store- the CAPTCHA store | 验证码存储analyzer- the behavior analyzer | 行为分析器
-
-
Method Details
-
recordCreation
-
validate
Description copied from interface:CaptchaValidatorValidates a CAPTCHA answer. 验证验证码答案。- Specified by:
validatein interfaceCaptchaValidator- Parameters:
id- the CAPTCHA ID | 验证码 IDanswer- the provided answer | 提供的答案- Returns:
- the validation result | 验证结果
-
validate
Description copied from interface:CaptchaValidatorValidates a CAPTCHA answer with case sensitivity option. 验证验证码答案(带大小写敏感选项)。- Specified by:
validatein interfaceCaptchaValidator- Parameters:
id- the CAPTCHA ID | 验证码 IDanswer- the provided answer | 提供的答案caseSensitive- whether case sensitive | 是否区分大小写- Returns:
- the validation result | 验证结果
-
validate
Validates with explicit client ID. 使用明确的客户端 ID 进行验证。- Parameters:
id- the CAPTCHA ID | 验证码 IDanswer- the provided answer | 提供的答案clientId- the client identifier | 客户端标识符- Returns:
- the validation result | 验证结果
-
validate
Validates with explicit client ID and case sensitivity. 使用明确的客户端 ID 和大小写敏感选项进行验证。- Parameters:
id- the CAPTCHA ID | 验证码 IDanswer- 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 | 验证码 IDanswer- 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
HUMANorINSUFFICIENT_DATA), the validation is rejected with a suspicious behavior result.当提供轨迹数据时,首先进行轨迹分析。如果轨迹表明机器人行为 (任何非
HUMAN或INSUFFICIENT_DATA的结果), 验证将以可疑行为结果被拒绝。- Parameters:
id- the CAPTCHA ID | 验证码 IDanswer- the provided answer | 提供的答案clientId- the client identifier | 客户端标识符trajectory- the trajectory data, may be null | 轨迹数据,可以为 nullcaseSensitive- whether case sensitive | 是否区分大小写- Returns:
- the validation result | 验证结果
- Since:
- JDK 25, opencode-base-captcha V1.0.3
-
getAnalyzer
Gets the behavior analyzer. 获取行为分析器。- Returns:
- the behavior analyzer | 行为分析器
-
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. 清除旧的创建记录。
-