java.lang.Object
org.miaixz.bus.extra.captcha.strategy.MathStrategy
- All Implemented Interfaces:
Serializable,CodeStrategy
Math calculation CAPTCHA generation strategy.
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newMathStrategywith default settings.MathStrategy(int numberLength, boolean resultHasNegativeNumber) Constructs a newMathStrategywith the specified number length and negative result allowance. -
Method Summary
Modifier and TypeMethodDescriptiongenerate()Generates the CAPTCHA code.intGets the length of the CAPTCHA code string (including numbers, operator, and equals sign).booleanVerifies if the user's input string matches the generated CAPTCHA code.
-
Constructor Details
-
MathStrategy
public MathStrategy()Constructs a newMathStrategywith default settings. Uses a number length of 2 and does not allow negative results. -
MathStrategy
public MathStrategy(int numberLength, boolean resultHasNegativeNumber) Constructs a newMathStrategywith the specified number length and negative result allowance.- Parameters:
numberLength- The maximum number of digits for numbers involved in the calculation.resultHasNegativeNumber-trueif the calculation result can be negative,falseotherwise.
-
-
Method Details
-
generate
Description copied from interface:CodeStrategyGenerates the CAPTCHA code.- Specified by:
generatein interfaceCodeStrategy- Returns:
- The CAPTCHA code.
-
verify
Description copied from interface:CodeStrategyVerifies if the user's input string matches the generated CAPTCHA code. Users can define their own matching logic by implementing this method.- Specified by:
verifyin interfaceCodeStrategy- 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.
-