Captcha is used to prevent automated requests and bots.
- The
CaptchaUtilclass generates image-based captchas. - Captchas are encoded in Base64 and sent to the client.
- The client must solve the captcha to proceed with certain requests.
CaptchaUtil: Generates and encodes captchas.
Captcha captcha = CaptchaUtil.createCaptcha(200, 50);
String base64Image = CaptchaUtil.encodeCaptcha(captcha);Creates a captcha image and encodes it to Base64 for sending to the client.
// CaptchaUtil.java
public static Captcha createCaptcha(int width, int height){
return new Captcha.Builder(width, height)
.addText()
.addBackground()
.addNoise()
.build();
}Generates a captcha image with text, background, and noise.
// CaptchaUtil.java
public static String encodeCaptcha(Captcha c){
try(ByteArrayOutputStream o = new ByteArrayOutputStream()){
ImageIO.write(c.getImage(), "png", o);
return Base64.getEncoder().encodeToString(o.toByteArray());
} catch (IOException e){
throw new RuntimeException("error encoding Captcha", e);
}
}Encodes the captcha image to a Base64 string for easy transmission.