How to Implement Secure and Stylish Captchas with Mica‑Captcha in Spring Boot
This guide explains how to add the Mica‑Captcha library to a Spring Boot project, covering Maven/Gradle dependencies, configuration options, cache setup, servlet and WebFlux usage, and generating both image and Base64 captchas for secure, attractive verification.
Captcha Effect
Ensures captcha security while maintaining aesthetics; feel free to try it, more usage examples are available in the mica demo project.
Dependency Coordinates
Maven
<dependency>
<groupId>net.dreamlu</groupId>
<artifactId>mica-captcha</artifactId>
</dependency>Gradle
implementation "net.dreamlu:mica-captcha"Configuration Items
Configuration Item
Default Value
Description
mica.captcha.cache-name
micaCaptchaCache
Name of the captcha cache (default: micaCaptchaCache)
mica.captcha.cookie-name
mica-captcha
Name of the captcha cookie (default: mica-captcha)
mica.captcha.enabled
true
Whether the captcha is enabled (default: true)
Note: When used with the mica-cache-redis component from mica‑pro, the cache name can be automatically configured with a timeout using the # placeholder.
Usage
First configure a Spring cache (e.g., EhCache, Redis) and set a timeout for micaCaptchaCache.
Servlet
Generate captcha
@Autowired
private MicaCaptchaServlet micaCaptcha;
/**
* Image captcha endpoint
*/
@GetMapping(value = "/captcha.jpg", produces = MediaType.IMAGE_JPEG_VALUE)
@ResponseBody
public ResponseEntity<Resource> captcha(HttpServletResponse response) {
return micaCaptcha.generate(response);
}Validate captcha
boolean validated = micaCaptcha.validate(response, userInputCaptcha);WebFlux
Generate captcha
@Autowired
private MicaCaptchaReactive micaCaptcha;
/**
* Image captcha endpoint for WebFlux
*/
@GetMapping(value = "/captcha.jpg", produces = MediaType.IMAGE_JPEG_VALUE)
@ResponseBody
public Mono<ResponseEntity<Resource>> captcha(ServerWebExchange exchange) {
return Mono.just(micaCaptcha.generate(exchange));
}Validate captcha
boolean validated = micaCaptcha.validate(exchange, userInputCaptcha);Generate Other Types of Captchas
micaCaptcha.generateBase64– Generates a Base64‑encoded image captcha, suitable for mobile apps or front‑end/back‑end separation. micaCaptcha.validateBase64 – Validates a Base64‑encoded image captcha.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Architecture Diary
Committed to sharing original, high‑quality technical articles; no fluff or promotional content.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
