Skip to content

Commit 8727393

Browse files
committed
BE: Fix CORS once again (#3957)
(cherry picked from commit 9549f68)
1 parent 26464ba commit 8727393

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed
Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
package com.provectus.kafka.ui.config;
22

3+
import org.springframework.context.annotation.Bean;
34
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.http.HttpHeaders;
6+
import org.springframework.http.HttpMethod;
7+
import org.springframework.http.HttpStatus;
8+
import org.springframework.http.server.reactive.ServerHttpRequest;
9+
import org.springframework.http.server.reactive.ServerHttpResponse;
410
import org.springframework.web.reactive.config.CorsRegistry;
511
import org.springframework.web.reactive.config.WebFluxConfigurer;
12+
import org.springframework.web.server.ServerWebExchange;
13+
import org.springframework.web.server.WebFilter;
14+
import org.springframework.web.server.WebFilterChain;
15+
import reactor.core.publisher.Mono;
616

717
@Configuration
8-
public class CorsGlobalConfiguration implements WebFluxConfigurer {
18+
public class CorsGlobalConfiguration {
919

10-
@Override
11-
public void addCorsMappings(CorsRegistry registry) {
12-
registry.addMapping("/**")
13-
.allowedOrigins("*")
14-
.allowedMethods("*")
15-
.allowedHeaders("*")
16-
.allowCredentials(false);
20+
@Bean
21+
public WebFilter corsFilter() {
22+
return (final ServerWebExchange ctx, final WebFilterChain chain) -> {
23+
final ServerHttpRequest request = ctx.getRequest();
24+
25+
final ServerHttpResponse response = ctx.getResponse();
26+
final HttpHeaders headers = response.getHeaders();
27+
headers.add("Access-Control-Allow-Origin", "*");
28+
headers.add("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
29+
headers.add("Access-Control-Max-Age", "3600");
30+
headers.add("Access-Control-Allow-Headers", "Content-Type");
31+
32+
if (request.getMethod() == HttpMethod.OPTIONS) {
33+
response.setStatusCode(HttpStatus.OK);
34+
return Mono.empty();
35+
}
36+
37+
return chain.filter(ctx);
38+
};
1739
}
40+
1841
}

0 commit comments

Comments
 (0)