|
9 | 9 | import com.fasterxml.jackson.databind.ObjectMapper; |
10 | 10 | import lombok.RequiredArgsConstructor; |
11 | 11 | import org.apache.commons.lang3.StringUtils; |
| 12 | +import org.springframework.http.HttpHeaders; |
12 | 13 | import org.springframework.http.HttpMethod; |
13 | 14 | import org.springframework.http.HttpStatus; |
14 | 15 | import org.springframework.http.ProblemDetail; |
@@ -65,14 +66,14 @@ public Mono<Void> filter(@NonNull ServerWebExchange exchange, @NonNull WebFilter |
65 | 66 | private Mono<Void> sendCaptchaRequiredResponse(ServerWebExchange exchange, |
66 | 67 | SettingConfigGetter.CaptchaConfig captchaConfig, |
67 | 68 | ResponseStatusException e) { |
68 | | - exchange.getResponse().getHeaders().addIfAbsent(CAPTCHA_REQUIRED_HEADER, "true"); |
| 69 | + addHeaderIfAbsent(exchange.getResponse().getHeaders(), CAPTCHA_REQUIRED_HEADER, Boolean.TRUE.toString()); |
69 | 70 | exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN); |
70 | 71 | return captchaManager.generate(exchange, captchaConfig) |
71 | 72 | .flatMap(captcha -> { |
72 | 73 | var problemDetail = toProblemDetail(e); |
73 | 74 | problemDetail.setProperty("captcha", captcha.imageBase64()); |
74 | 75 | var responseData = getResponseData(problemDetail); |
75 | | - exchange.getResponse().getHeaders().addIfAbsent("Content-Type", CONTENT_TYPE); |
| 76 | + addHeaderIfAbsent(exchange.getResponse().getHeaders(), HttpHeaders.CONTENT_TYPE, CONTENT_TYPE); |
76 | 77 | return exchange.getResponse() |
77 | 78 | .writeWith(Mono.just(exchange.getResponse().bufferFactory().wrap(responseData))); |
78 | 79 | }); |
@@ -151,4 +152,17 @@ Mono<Boolean> isAnonymousCommenter(ServerWebExchange exchange) { |
151 | 152 | public int getOrder() { |
152 | 153 | return SecurityWebFiltersOrder.AUTHORIZATION.getOrder(); |
153 | 154 | } |
| 155 | + |
| 156 | + /** |
| 157 | + * Adds a header to the HttpHeaders if it is not already present. Only for forward-compatibility with Spring Framework 7. |
| 158 | + * |
| 159 | + * @param headers the HttpHeaders to add the header to |
| 160 | + * @param headerName the name of the header |
| 161 | + * @param headerValue the value of the header |
| 162 | + */ |
| 163 | + private static void addHeaderIfAbsent(HttpHeaders headers, String headerName, String headerValue) { |
| 164 | + if (headers.getFirst(headerName) == null) { |
| 165 | + headers.add(headerName, headerValue); |
| 166 | + } |
| 167 | + } |
154 | 168 | } |
0 commit comments