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.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
How to Implement Secure and Stylish Captchas with Mica‑Captcha in Spring Boot

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Captchaspring-boot
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.