Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:error log 추가 #12

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ private Mono<Void> onError(ServerWebExchange exchange, Class<? extends Throwable
byte[] bytes = objectMapper.writeValueAsBytes(responseBody);
wrap = exchange.getResponse().bufferFactory().wrap(bytes);
} catch (JsonProcessingException e) {
log.error("Error while processing JSON response:", e);
e.printStackTrace();
}

response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
return exchange.getResponse().writeWith(Flux.just(wrap));
}

Expand Down
104 changes: 52 additions & 52 deletions src/main/java/gwangjang/server/global/filter/CorsConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
//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);
// };
// }
//
//}
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);
};
}

}
18 changes: 6 additions & 12 deletions src/main/java/gwangjang/server/global/filter/GlobalFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
}));

};
}
Expand Down