Skip to content

Commit

Permalink
hotfix: add cors filter
Browse files Browse the repository at this point in the history
  • Loading branch information
CChuYong committed Jun 26, 2024
1 parent 3970842 commit 49e5115
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package kr.mafoo.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.cors.reactive.CorsUtils;
import org.springframework.web.server.WebFilter;
import reactor.core.publisher.Mono;

@Configuration
public class CorsConfigurationFilter {
@Bean
public WebFilter corsFilter() {
return (ctx, chain) -> {
ServerHttpRequest request = ctx.getRequest();

if(CorsUtils.isPreFlightRequest(request)) {
ServerHttpResponse response = ctx.getResponse();
HttpHeaders headers = response.getHeaders();
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS");
headers.add("Access-Control-Max-Age", "3600");
headers.add("Access-Control-Allow-Headers", "Authorization, Content-Type");
headers.add("Access-Control-Allow-Credentials", "true");

if (request.getMethod() == HttpMethod.OPTIONS) {
response.setStatusCode(HttpStatus.OK);
return Mono.empty();
}
}

return chain.filter(ctx);
};
}
}

0 comments on commit 49e5115

Please sign in to comment.