Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ProblemDetail;
Expand Down Expand Up @@ -65,14 +66,14 @@ public Mono<Void> filter(@NonNull ServerWebExchange exchange, @NonNull WebFilter
private Mono<Void> sendCaptchaRequiredResponse(ServerWebExchange exchange,
SettingConfigGetter.CaptchaConfig captchaConfig,
ResponseStatusException e) {
exchange.getResponse().getHeaders().addIfAbsent(CAPTCHA_REQUIRED_HEADER, "true");
addHeaderIfAbsent(exchange.getResponse().getHeaders(), CAPTCHA_REQUIRED_HEADER, Boolean.TRUE.toString());
exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
return captchaManager.generate(exchange, captchaConfig)
.flatMap(captcha -> {
var problemDetail = toProblemDetail(e);
problemDetail.setProperty("captcha", captcha.imageBase64());
var responseData = getResponseData(problemDetail);
exchange.getResponse().getHeaders().addIfAbsent("Content-Type", CONTENT_TYPE);
addHeaderIfAbsent(exchange.getResponse().getHeaders(), HttpHeaders.CONTENT_TYPE, CONTENT_TYPE);
return exchange.getResponse()
.writeWith(Mono.just(exchange.getResponse().bufferFactory().wrap(responseData)));
});
Expand Down Expand Up @@ -151,4 +152,17 @@ Mono<Boolean> isAnonymousCommenter(ServerWebExchange exchange) {
public int getOrder() {
return SecurityWebFiltersOrder.AUTHORIZATION.getOrder();
}

/**
* Adds a header to the HttpHeaders if it is not already present. Only for forward-compatibility with Spring Framework 7.
*
* @param headers the HttpHeaders to add the header to
* @param headerName the name of the header
* @param headerValue the value of the header
*/
private static void addHeaderIfAbsent(HttpHeaders headers, String headerName, String headerValue) {
if (headers.getFirst(headerName) == null) {
headers.add(headerName, headerValue);
}
}
}
Loading