Class JigsawCaptchaGenerator
java.lang.Object
cloud.opencode.base.captcha.generator.AbstractCaptchaGenerator
cloud.opencode.base.captcha.interactive.JigsawCaptchaGenerator
- All Implemented Interfaces:
CaptchaGenerator
public final class JigsawCaptchaGenerator
extends AbstractCaptchaGenerator
implements CaptchaGenerator
Jigsaw Captcha Generator — Generates puzzle piece ordering CAPTCHA
拼接验证码生成器 — 生成碎片排序验证码
This generator creates a jigsaw-style interactive CAPTCHA where an image is split into a grid of pieces that are shuffled. The user must arrange the pieces back into the correct order.
此生成器创建拼图式交互验证码,将图像切割成网格碎片并打乱顺序。 用户需要将碎片重新排列为正确顺序。
Features | 主要功能:
- Grid-based puzzle piece ordering - 基于网格的碎片排序
- Procedurally generated background (no external images) - 程序生成背景(无需外部图片)
- Fisher-Yates shuffle with guaranteed displacement - Fisher-Yates 洗牌确保至少一个碎片移位
- Base64-encoded piece images in metadata - 元数据中包含 Base64 编码的碎片图像
- Configurable grid size (default 3x3) - 可配置网格大小(默认 3x3)
Usage Examples | 使用示例:
CaptchaGenerator gen = new JigsawCaptchaGenerator();
Captcha captcha = gen.generate(config);
// Retrieve shuffled piece images from metadata
List<String> pieces = (List<String>) captcha.metadata().get("pieces");
int gridSize = (int) captcha.metadata().get("gridSize");
// User submits answer as comma-separated positions: "1,2,0,..."
// Answer represents the position of each original piece in the shuffled layout
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是(无状态)
- Null-safe: No (config must not be null) - 空值安全: 否(配置不能为null)
- Low entropy: A 3x3 grid has 9! = 362,880 permutations. Must be used with BehaviorAnalyzer and CaptchaRateLimiter for adequate protection. 低熵: 3x3 网格有 9! = 362,880 种排列。必须配合 BehaviorAnalyzer 和 CaptchaRateLimiter 使用。
Performance | 性能:
- Image generation is O(width * height) for background rendering - 背景渲染时间复杂度 O(width * height)
- Piece cutting and encoding is O(gridSize^2 * pieceArea) - 碎片切割和编码 O(gridSize^2 * pieceArea)
- Each piece is independently PNG-encoded for transport - 每个碎片独立 PNG 编码以便传输
- Since:
- JDK 25, opencode-base-captcha V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongenerate(CaptchaConfig config) Generates a CAPTCHA with the specified configuration.getType()Gets the supported CAPTCHA type.Methods inherited from class AbstractCaptchaGenerator
buildCaptcha, createGraphics, createImage, createMetadata, drawNoise, generateId, toBytesMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface CaptchaGenerator
generate
-
Constructor Details
-
JigsawCaptchaGenerator
public JigsawCaptchaGenerator()
-
-
Method Details
-
getType
Description copied from interface:CaptchaGeneratorGets the supported CAPTCHA type. 获取支持的验证码类型。- Specified by:
getTypein interfaceCaptchaGenerator- Returns:
- the CAPTCHA type | 验证码类型
-
generate
Description copied from interface:CaptchaGeneratorGenerates a CAPTCHA with the specified configuration. 使用指定配置生成验证码。- Specified by:
generatein interfaceCaptchaGenerator- Parameters:
config- the configuration | 配置- Returns:
- the generated CAPTCHA | 生成的验证码
-