diff --git a/src/main/java/gwangjang/server/global/filter/AuthorizationHeaderFilter.java b/src/main/java/gwangjang/server/global/filter/AuthorizationHeaderFilter.java index b4138e0..2519fe8 100644 --- a/src/main/java/gwangjang/server/global/filter/AuthorizationHeaderFilter.java +++ b/src/main/java/gwangjang/server/global/filter/AuthorizationHeaderFilter.java @@ -153,9 +153,9 @@ private Mono onError(ServerWebExchange exchange, Class { -// ServerHttpRequest request = ctx.getRequest(); -// if (CorsUtils.isCorsRequest(request) || CorsUtils.isPreFlightRequest(request) ) { -// log.info("corsFilter ongoing !"); -// ServerHttpResponse response = ctx.getResponse(); -// HttpHeaders headers = response.getHeaders(); -// headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN); -// headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS); -// headers.add("Access-Control-Max-Age", MAX_AGE); -// headers.add("Access-Control-Allow-Headers",ALLOWED_HEADERS); -//// response.setStatusCode(HttpStatus.OK); -// if (request.getMethod() == HttpMethod.OPTIONS ) { -// response.setStatusCode(HttpStatus.OK); -// return Mono.empty(); -// } -// log.info(response.getStatusCode().toString()); -// -// } -// return chain.filter(ctx); -// }; -// } -// -//} +package gwangjang.server.global.filter; + +import lombok.extern.slf4j.Slf4j; +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.ServerWebExchange; +import org.springframework.web.server.WebFilter; +import org.springframework.web.server.WebFilterChain; + +import reactor.core.publisher.Mono; + +@Slf4j + +@Configuration +public class CorsConfiguration { + + private static final String ALLOWED_HEADERS = "x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN"; + private static final String ALLOWED_METHODS = "GET, PUT, POST, DELETE, OPTIONS"; + private static final String ALLOWED_ORIGIN = "*"; + private static final String MAX_AGE = "3600"; + + @Bean + public WebFilter corsFilter() { + return (ServerWebExchange ctx, WebFilterChain chain) -> { + ServerHttpRequest request = ctx.getRequest(); + if (CorsUtils.isCorsRequest(request) || CorsUtils.isPreFlightRequest(request) ) { + log.info("corsFilter ongoing !"); + ServerHttpResponse response = ctx.getResponse(); + HttpHeaders headers = response.getHeaders(); + headers.add("Access-Control-Allow-Origin", ALLOWED_ORIGIN); + headers.add("Access-Control-Allow-Methods", ALLOWED_METHODS); + headers.add("Access-Control-Max-Age", MAX_AGE); + headers.add("Access-Control-Allow-Headers",ALLOWED_HEADERS); + // response.setStatusCode(HttpStatus.OK); + if (request.getMethod() == HttpMethod.OPTIONS ) { + response.setStatusCode(HttpStatus.OK); + return Mono.empty(); + } + log.info(response.getStatusCode().toString()); + + } + return chain.filter(ctx); + }; + } + +} \ No newline at end of file diff --git a/src/main/java/gwangjang/server/global/filter/GlobalFilter.java b/src/main/java/gwangjang/server/global/filter/GlobalFilter.java index 1cf7513..3d92cf7 100644 --- a/src/main/java/gwangjang/server/global/filter/GlobalFilter.java +++ b/src/main/java/gwangjang/server/global/filter/GlobalFilter.java @@ -32,20 +32,14 @@ public GatewayFilter apply(Config config) { log.info("Global Filter Start: request path -> {}" , request.getPath()); } - // 예제: /token/** 경로에 대한 요청일 때만 실행 - if (request.getPath().toString().startsWith("/token/")) { - log.info("Global com.example.scg.filter baseMessgae: {}", config.getBaseMessage()); - // Your global filter logic here + // Global Post Filter + //Mono는 webflux에서 단일값 전송할때 Mono값으로 전송 + return chain.filter(exchange).then(Mono.fromRunnable(()->{ - //Mono는 webflux에서 단일값 전송할때 Mono값으로 전송 - return chain.filter(exchange).then(Mono.fromRunnable(() -> { + if (config.isPostLogger()){ log.info("Global Filter End: response status code -> {}" , response.getStatusCode()); - })); - } else { - // /token/** 경로가 아니면 그냥 다음 필터로 이동 - return chain.filter(exchange); - } - + } + })); }; }