overview

Origin

Due to the huge demand for CAPTCHA and the previous accumulation in my project, CAPTCHA generation and verification functions were added to Hutool.

Introduction

The CAPTCHA function is located in the cn.hutool.captcha package, with the core interface being ICaptcha. This interface defines the following methods:

  • createCode: creates CAPTCHA, implementation classes need to generate both random CAPTCHA string and CAPTCHA image
  • getCode: gets the text content of the CAPTCHA
  • verify: verifies whether the CAPTCHA is correct, it is recommended to ignore case sensitivity
  • write: writes the CAPTCHA to the target stream

Among them, the write method only has one OutputStream, and implementation classes of ICaptcha can wrap methods to write to files or other targets based on this method.

AbstractCaptcha is an abstract implementation class of ICaptcha. This class implements methods such as text generation, case-insensitive verification, writing to streams and files, etc. By inheriting this abstract class, it is only necessary to implement the createImage method to define the rules of image generation.

Implementation Classes

LineCaptcha: Line Interruption CAPTCHA

Generation effect is roughly as follows: (image link: https://static.oschina.net/uploads/img/201712/16113708_B8Hu.png)

Example:

// Define the length and width of the captcha
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);

// Write the captcha to a file, or to a stream
lineCaptcha.write("d:/line.png");
// Output the code
Console.log(lineCaptcha.getCode());
// Verify the captcha's validity, return a boolean value
lineCaptcha.verify("1234");

// Re-generate the captcha
lineCaptcha.createCode();
lineCaptcha.write("d:/line.png");
// New captcha code
Console.log(lineCaptcha.getCode());
// Verify the captcha's validity, return a boolean value
lineCaptcha.verify("1234");


### `CircleCaptcha` Circle Interruption CAPTCHA

(image link: https://static.oschina.net/uploads/img/201712/16113738_eqt9.png)

Example:

```java
// Define the length, width, captcha character count, and interfere in element count of the captcha
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
//CircleCaptcha captcha = new CircleCaptcha(200, 100, 4, 20);
// Write the captcha to a file, or to a stream
captcha.write("d:/circle.png");
// Verify the captcha's validity, return a boolean value
captcha.verify("1234");

ShearCaptcha Shear Interference CAPTCHA

(image link: https://static.oschina.net/uploads/img/201712/16113807_sICp.png)

Example:

// Define the length, width, captcha character count, and interfere in line width of the captcha
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
//ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
// Write the captcha to a file, or to a stream
captcha.write("d:/shear.png");
// Verify the captcha's validity, return a boolean value
captcha.verify("1234");

Write to the browser (Servlet output)

ICaptcha captcha = ...;
captcha.write(response.getOutputStream());
// Remember to close the Servlet's output stream!

Customizing CAPTCHA

Sometimes standard CAPTCHAs don’t meet requirements, such as using pure alphabet CAPTCHAs, pure numeric CAPTCHAs, or CAPTCHAs with arithmetic operations. In these cases, we need to customize the CodeGenerator:

// Customize a pure numeric CAPTCHA (4 random digits, can have duplicates)
RandomGenerator randomGenerator = new RandomGenerator("0123456789", 4);
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
lineCaptcha.setGenerator(randomGenerator);
// Generate a new code
lineCaptcha.createCode();
ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 45, 4, 4);
// Customize the CAPTCHA content to include arithmetic operations
captcha.setGenerator(new MathGenerator());
// Generate a new code
captcha.createCode();