Giriş
GlobalFilter ve GatewayFilter benziyorlar
Örnek
Şöyle yaparız
import org.springframework.cloud.gateway.filter.GatewayFilterChain;import org.springframework.cloud.gateway.filter.GlobalFilter;import org.springframework.core.Ordered;import org.springframework.core.io.buffer.DataBuffer;import org.springframework.http.HttpStatus;import org.springframework.http.server.reactive.ServerHttpResponse;import org.springframework.web.server.ServerWebExchange;import reactor.core.publisher.Mono;import java.nio.charset.StandardCharsets;public class PostGlobalFilter implements GlobalFilter, Ordered {...@Overridepublic int getOrder() {return 0;}}
Metodun içi şöyle
@Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { return chain.filter(exchange).then(Mono.fromRunnable(()->{ ServerHttpResponse response = exchange.getResponse(); HttpStatus responseStatus = response.getStatusCode(); if(responseStatus.equals(HttpStatus.BAD_REQUEST)){ String newResponseBody = "<body>\n" + "<h1 style=\"color:red;text-align:center\">Bad Request </h1>\n" + "<p>If you are seeing this page it means response body is modified.</p>\n" + "</body>"; DataBuffer dataBuffer = response.bufferFactory() .wrap(newResponseBody.getBytes(StandardCharsets.UTF_8)); response.getHeaders().setContentLength(newResponseBody.length()); response.writeWith(Mono.just(dataBuffer)).subscribe(); exchange.mutate().response(response).build(); } })); }
Hiç yorum yok:
Yorum Gönder