|
1 | 1 | package com.provectus.kafka.ui.config; |
2 | 2 |
|
| 3 | +import org.springframework.context.annotation.Bean; |
3 | 4 | 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; |
4 | 10 | import org.springframework.web.reactive.config.CorsRegistry; |
5 | 11 | 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; |
6 | 16 |
|
7 | 17 | @Configuration |
8 | | -public class CorsGlobalConfiguration implements WebFluxConfigurer { |
| 18 | +public class CorsGlobalConfiguration { |
9 | 19 |
|
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 | + }; |
17 | 39 | } |
| 40 | + |
18 | 41 | } |
0 commit comments