Module bus.extra

Class MathStrategy

java.lang.Object
org.miaixz.bus.extra.captcha.strategy.MathStrategy
All Implemented Interfaces:
Serializable, CodeStrategy

public class MathStrategy extends Object implements CodeStrategy
Math calculation CAPTCHA generation strategy.
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new MathStrategy with default settings.
    MathStrategy(int numberLength, boolean resultHasNegativeNumber)
    Constructs a new MathStrategy with the specified number length and negative result allowance.
  • Method Summary

    Modifier and Type
    Method
    Description
    Generates the CAPTCHA code.
    int
    Gets the length of the CAPTCHA code string (including numbers, operator, and equals sign).
    boolean
    verify(String code, String userInputCode)
    Verifies if the user's input string matches the generated CAPTCHA code.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MathStrategy

      public MathStrategy()
      Constructs a new MathStrategy with default settings. Uses a number length of 2 and does not allow negative results.
    • MathStrategy

      public MathStrategy(int numberLength, boolean resultHasNegativeNumber)
      Constructs a new MathStrategy with the specified number length and negative result allowance.
      Parameters:
      numberLength - The maximum number of digits for numbers involved in the calculation.
      resultHasNegativeNumber - true if the calculation result can be negative, false otherwise.
  • Method Details

    • generate

      public String generate()
      Description copied from interface: CodeStrategy
      Generates the CAPTCHA code.
      Specified by:
      generate in interface CodeStrategy
      Returns:
      The CAPTCHA code.
    • verify

      public boolean verify(String code, String userInputCode)
      Description copied from interface: CodeStrategy
      Verifies if the user's input string matches the generated CAPTCHA code. Users can define their own matching logic by implementing this method.
      Specified by:
      verify in interface CodeStrategy
      Parameters:
      code - The randomly generated CAPTCHA code.
      userInputCode - The CAPTCHA code entered by the user.
      Returns:
      Whether the verification is successful.
    • getLength

      public int getLength()
      Gets the length of the CAPTCHA code string (including numbers, operator, and equals sign).
      Returns:
      The length of the CAPTCHA code.