From f0eab431ec1f724ff0e39a3437a880677b3e2e41 Mon Sep 17 00:00:00 2001 From: AlexBob <5199840@qq.com> Date: Fri, 31 May 2024 09:54:21 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20fix(captcha):=20corrected=20meth?= =?UTF-8?q?od=20calls=20in=20CaptchaController=20and=20CaptchaFilter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/captcha/CaptchaController.java | 2 +- .../security/core/captcha/CaptchaToken.java | 30 ++++++++----------- .../boot/security/filter/CaptchaFilter.java | 5 ++-- 3 files changed, 15 insertions(+), 22 deletions(-) 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); }