Skip to content

Commit

Permalink
🔨 fix(captcha): corrected method calls in CaptchaController and Captc…
Browse files Browse the repository at this point in the history
…haFilter
  • Loading branch information
vnobo committed May 31, 2024
1 parent 090a3fb commit f0eab43
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public Mono<ResponseEntity<DataBuffer>> 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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <a href="https://github.com/vnobo">Alex bob</a>
*/
@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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ private Mono<Void> validateToken(ServerWebExchange exchange) {
}

private Mono<Boolean> 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<String> 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);
}

Expand Down

0 comments on commit f0eab43

Please sign in to comment.