Skip to content

Commit

Permalink
[fix] 에러 메시지 json으로 파싱
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryeolee committed Oct 27, 2023
1 parent 4290f53 commit 10ebbe6
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

@Component
@Slf4j
public class JwtAuthenticationGatewayFilterFactory extends AbstractGatewayFilterFactory<JwtAuthenticationGatewayFilterFactory.Config> {
Expand Down Expand Up @@ -131,10 +135,14 @@ public String decode(String token) {
private Mono<Void> onError(ServerWebExchange exchange, String error, HttpStatus httpStatus) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(httpStatus);
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);

String errorMessage = "{\"error\": \"" + error + "\"}";
DataBuffer buffer = response.bufferFactory().wrap(errorMessage.getBytes(StandardCharsets.UTF_8));

log.error(error);

return response.setComplete();
return response.writeWith(Mono.just(buffer)).then(response.setComplete());
}

@Data
Expand Down

0 comments on commit 10ebbe6

Please sign in to comment.