Class OpenCaptcha

java.lang.Object
cloud.opencode.base.captcha.OpenCaptcha

public final class OpenCaptcha extends Object
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:
  • Method Details

    • create

      public static Captcha create()
      Creates a CAPTCHA with default configuration. 使用默认配置创建验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • create

      public static Captcha create(CaptchaConfig config)
      Creates a CAPTCHA with the specified configuration. 使用指定配置创建验证码。
      Parameters:
      config - the configuration | 配置
      Returns:
      the generated CAPTCHA | 生成的验证码
    • create

      public static Captcha create(CaptchaType type)
      Creates a CAPTCHA of the specified type. 创建指定类型的验证码。
      Parameters:
      type - the CAPTCHA type | 验证码类型
      Returns:
      the generated CAPTCHA | 生成的验证码
    • numeric

      public static Captcha numeric()
      Creates a numeric CAPTCHA. 创建数字验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • alpha

      public static Captcha alpha()
      Creates an alphabetic CAPTCHA. 创建字母验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • alphanumeric

      public static Captcha alphanumeric()
      Creates an alphanumeric CAPTCHA. 创建字母数字验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • arithmetic

      public static Captcha arithmetic()
      Creates an arithmetic CAPTCHA. 创建算术验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • chinese

      public static Captcha chinese()
      Creates a Chinese CAPTCHA. 创建中文验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • gif

      public static Captcha gif()
      Creates a GIF CAPTCHA. 创建 GIF 验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • slider

      public static Captcha slider()
      Creates a slider CAPTCHA. 创建滑块验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • click

      public static Captcha click()
      Creates a click CAPTCHA. 创建点击验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • rotate

      public static Captcha rotate()
      Creates a rotate CAPTCHA. 创建旋转验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • audio

      public static Captcha audio()
      Creates an audio CAPTCHA. 创建音频验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • jigsaw

      public static Captcha jigsaw()
      Creates a jigsaw CAPTCHA. 创建拼接验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • pow

      public static Captcha pow()
      Creates a PoW CAPTCHA. 创建工作量证明验证码。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • generate

      public Captcha generate()
      Generates a CAPTCHA and stores it. 生成验证码并存储。
      Returns:
      the generated CAPTCHA | 生成的验证码
    • generate

      public Captcha generate(CaptchaConfig config)
      Generates a CAPTCHA with configuration and stores it. 使用配置生成验证码并存储。
      Parameters:
      config - the configuration | 配置
      Returns:
      the generated CAPTCHA | 生成的验证码
    • validate

      public ValidationResult validate(String id, String answer)
      Validates a CAPTCHA answer. 验证验证码答案。
      Parameters:
      id - the CAPTCHA ID | 验证码 ID
      answer - the provided answer | 提供的答案
      Returns:
      the validation result | 验证结果
    • render

      public void render(Captcha captcha, OutputStream out) throws IOException
      Renders a CAPTCHA to an output stream. 将验证码渲染到输出流。
      Parameters:
      captcha - the CAPTCHA | 验证码
      out - the output stream | 输出流
      Throws:
      IOException - if rendering fails | 如果渲染失败
    • getStore

      public CaptchaStore getStore()
      Gets the store. 获取存储。
      Returns:
      the store | 存储
    • getConfig

      public CaptchaConfig getConfig()
      Gets the configuration. 获取配置。
      Returns:
      the configuration | 配置
    • getMetrics

      public CaptchaMetrics getMetrics()
      Gets the metrics collector, if configured. 获取指标收集器(如果已配置)。
      Returns:
      the metrics collector, or null | 指标收集器,或 null
    • getEventListener

      public CaptchaEventListener getEventListener()
      Gets the event listener, if configured. 获取事件监听器(如果已配置)。
      Returns:
      the event listener, or null | 事件监听器,或 null
    • builder

      public static OpenCaptcha.Builder builder()
      Creates a builder for OpenCaptcha. 创建 OpenCaptcha 的构建器。
      Returns:
      a new builder | 新构建器