Module bus.extra

Class RandomStrategy

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

public class RandomStrategy extends AbstractStrategy
Random character CAPTCHA generation strategy. Generates a random CAPTCHA string from a given base character set and length.
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • RandomStrategy

      public RandomStrategy(int count)
      Constructs a new RandomStrategy using letters and numbers as the base character set.
      Parameters:
      count - The length of the CAPTCHA code to generate.
    • RandomStrategy

      public RandomStrategy(String baseStr, int length)
      Constructs a new RandomStrategy with a custom base character set and length.
      Parameters:
      baseStr - The base character set from which to randomly select characters.
      length - The length of the CAPTCHA code to generate.
  • Method Details

    • generate

      public String generate()
      Description copied from interface: CodeStrategy
      Generates the CAPTCHA code.
      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.
      Parameters:
      code - The randomly generated CAPTCHA code.
      userInputCode - The CAPTCHA code entered by the user.
      Returns:
      Whether the verification is successful.