Class OpenCaptcha
java.lang.Object
cloud.opencode.base.captcha.OpenCaptcha
OpenCaptcha - Main entry point for CAPTCHA operations
OpenCaptcha - 验证码操作的主入口点
This class provides a simple API for generating and validating CAPTCHAs.
此类提供生成和验证验证码的简单 API。
Usage Examples | 使用示例:
// Simple usage
Captcha captcha = OpenCaptcha.create();
String base64 = captcha.toBase64DataUrl();
// With configuration
Captcha captcha = OpenCaptcha.create(CaptchaConfig.builder()
.type(CaptchaType.ARITHMETIC)
.width(200)
.height(80)
.build());
// Using builder for advanced usage
OpenCaptcha openCaptcha = OpenCaptcha.builder()
.store(CaptchaStore.memory())
.config(CaptchaConfig.defaults())
.build();
Captcha captcha = openCaptcha.generate();
ValidationResult result = openCaptcha.validate(id, answer);
Features | 主要功能:
- Static factory methods for quick CAPTCHA creation - 用于快速创建验证码的静态工厂方法
- Builder pattern for advanced configuration - 构建器模式用于高级配置
- Integrated store and validator lifecycle - 集成的存储和验证器生命周期
- Support for all CAPTCHA types (text, GIF, interactive) - 支持所有验证码类型(文本、GIF、交互式)
- Optional metrics collection (CaptchaMetrics) - 可选的指标收集
- Optional event listener (CaptchaEventListener) - 可选的事件监听器
Security | 安全性:
- Thread-safe: Yes (delegates to thread-safe components) - 线程安全: 是(委托给线程安全组件)
- Null-safe: No (parameters must be non-null) - 空值安全: 否(参数不能为空)
- Since:
- JDK 25, opencode-base-captcha V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classOpenCaptcha Builder OpenCaptcha 构建器 -
Method Summary
Modifier and TypeMethodDescriptionstatic Captchaalpha()Creates an alphabetic CAPTCHA.static CaptchaCreates an alphanumeric CAPTCHA.static CaptchaCreates an arithmetic CAPTCHA.static Captchaaudio()Creates an audio CAPTCHA.static OpenCaptcha.Builderbuilder()Creates a builder for OpenCaptcha.static Captchachinese()Creates a Chinese CAPTCHA.static Captchaclick()Creates a click CAPTCHA.static Captchacreate()Creates a CAPTCHA with default configuration.static Captchacreate(CaptchaConfig config) Creates a CAPTCHA with the specified configuration.static Captchacreate(CaptchaType type) Creates a CAPTCHA of the specified type.generate()Generates a CAPTCHA and stores it.generate(CaptchaConfig config) Generates a CAPTCHA with configuration and stores it.Gets the configuration.Gets the event listener, if configured.Gets the metrics collector, if configured.getStore()Gets the store.static Captchagif()Creates a GIF CAPTCHA.static Captchajigsaw()Creates a jigsaw CAPTCHA.static Captchanumeric()Creates a numeric CAPTCHA.static Captchapow()Creates a PoW CAPTCHA.voidrender(Captcha captcha, OutputStream out) Renders a CAPTCHA to an output stream.static Captcharotate()Creates a rotate CAPTCHA.static Captchaslider()Creates a slider CAPTCHA.Validates a CAPTCHA answer.
-
Method Details
-
create
Creates a CAPTCHA with default configuration. 使用默认配置创建验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
create
Creates a CAPTCHA with the specified configuration. 使用指定配置创建验证码。- Parameters:
config- the configuration | 配置- Returns:
- the generated CAPTCHA | 生成的验证码
-
create
Creates a CAPTCHA of the specified type. 创建指定类型的验证码。- Parameters:
type- the CAPTCHA type | 验证码类型- Returns:
- the generated CAPTCHA | 生成的验证码
-
numeric
Creates a numeric CAPTCHA. 创建数字验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
alpha
Creates an alphabetic CAPTCHA. 创建字母验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
alphanumeric
Creates an alphanumeric CAPTCHA. 创建字母数字验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
arithmetic
Creates an arithmetic CAPTCHA. 创建算术验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
chinese
Creates a Chinese CAPTCHA. 创建中文验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
gif
Creates a GIF CAPTCHA. 创建 GIF 验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
slider
Creates a slider CAPTCHA. 创建滑块验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
click
Creates a click CAPTCHA. 创建点击验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
rotate
Creates a rotate CAPTCHA. 创建旋转验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
audio
Creates an audio CAPTCHA. 创建音频验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
jigsaw
Creates a jigsaw CAPTCHA. 创建拼接验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
pow
Creates a PoW CAPTCHA. 创建工作量证明验证码。- Returns:
- the generated CAPTCHA | 生成的验证码
-
generate
Generates a CAPTCHA and stores it. 生成验证码并存储。- Returns:
- the generated CAPTCHA | 生成的验证码
-
generate
Generates a CAPTCHA with configuration and stores it. 使用配置生成验证码并存储。- Parameters:
config- the configuration | 配置- Returns:
- the generated CAPTCHA | 生成的验证码
-
validate
Validates a CAPTCHA answer. 验证验证码答案。- Parameters:
id- the CAPTCHA ID | 验证码 IDanswer- the provided answer | 提供的答案- Returns:
- the validation result | 验证结果
-
render
Renders a CAPTCHA to an output stream. 将验证码渲染到输出流。- Parameters:
captcha- the CAPTCHA | 验证码out- the output stream | 输出流- Throws:
IOException- if rendering fails | 如果渲染失败
-
getStore
-
getConfig
-
getMetrics
Gets the metrics collector, if configured. 获取指标收集器(如果已配置)。- Returns:
- the metrics collector, or null | 指标收集器,或 null
-
getEventListener
Gets the event listener, if configured. 获取事件监听器(如果已配置)。- Returns:
- the event listener, or null | 事件监听器,或 null
-
builder
Creates a builder for OpenCaptcha. 创建 OpenCaptcha 的构建器。- Returns:
- a new builder | 新构建器
-