diff --git a/boot/platform/src/main/java/com/platform/boot/security/core/captcha/CaptchaController.java b/boot/platform/src/main/java/com/platform/boot/security/core/captcha/CaptchaController.java index b5149c80..516d3096 100644 --- a/boot/platform/src/main/java/com/platform/boot/security/core/captcha/CaptchaController.java +++ b/boot/platform/src/main/java/com/platform/boot/security/core/captcha/CaptchaController.java @@ -32,7 +32,7 @@ public Mono> getCaptcha(ServerWebExchange exchange) { return this.captchaTokenRepository.generateToken(exchange) .publishOn(Schedulers.boundedElastic()).flatMap(captchaToken -> { try (OutputStream outputStream = dataBuffer.asOutputStream()) { - outputStream.write(captchaToken.getCaptcha().getBytes()); + outputStream.write(captchaToken.captcha().getBytes()); return Mono.just(ResponseEntity.ok().contentType(MediaType.IMAGE_PNG).body(dataBuffer)) .delayUntil((a) -> this.captchaTokenRepository.saveToken(exchange, captchaToken)); } catch (IOException e) { diff --git a/boot/platform/src/main/java/com/platform/boot/security/core/captcha/CaptchaToken.java b/boot/platform/src/main/java/com/platform/boot/security/core/captcha/CaptchaToken.java index 8ef4eb2b..90dc3647 100644 --- a/boot/platform/src/main/java/com/platform/boot/security/core/captcha/CaptchaToken.java +++ b/boot/platform/src/main/java/com/platform/boot/security/core/captcha/CaptchaToken.java @@ -1,28 +1,22 @@ package com.platform.boot.security.core.captcha; -import lombok.Data; +import org.springframework.util.Assert; import java.io.Serializable; /** - * captcha token entity class - * + * @param headerName http captcha header name + * @param parameterName http captcha parameter name + * @param captcha captcha * @author Alex bob */ -@Data(staticConstructor = "of") -public class CaptchaToken implements Serializable { - /** - * http captcha header name - */ - private final String headerName; +public record CaptchaToken(String headerName, String parameterName, String captcha) implements Serializable { + public static CaptchaToken of(String headerName, String parameterName, String captcha) { + return new CaptchaToken(headerName, parameterName, captcha); + } - /** - * http captcha parameter name - */ - private final String parameterName; - - /** - * captcha - */ - private final String captcha; + public Boolean validate(String code) { + Assert.notNull(code, "captcha code must not be null"); + return this.captcha.equalsIgnoreCase(code); + } } \ No newline at end of file diff --git a/boot/platform/src/main/java/com/platform/boot/security/filter/CaptchaFilter.java b/boot/platform/src/main/java/com/platform/boot/security/filter/CaptchaFilter.java index f8125a16..c905e885 100644 --- a/boot/platform/src/main/java/com/platform/boot/security/filter/CaptchaFilter.java +++ b/boot/platform/src/main/java/com/platform/boot/security/filter/CaptchaFilter.java @@ -65,12 +65,11 @@ private Mono validateToken(ServerWebExchange exchange) { } private Mono containsValidCaptchaToken(ServerWebExchange exchange, CaptchaToken captchaToken) { - return this.resolveCaptchaTokenValue(exchange, captchaToken) - .map((actual) -> captchaToken.getCaptcha().equals(actual)); + return this.resolveCaptchaTokenValue(exchange, captchaToken).map(captchaToken::validate); } private Mono resolveCaptchaTokenValue(ServerWebExchange exchange, CaptchaToken captchaToken) { - String captchaCode = exchange.getRequest().getHeaders().getFirst(captchaToken.getHeaderName()); + String captchaCode = exchange.getRequest().getHeaders().getFirst(captchaToken.headerName()); return Mono.justOrEmpty(captchaCode); }