Home > Back-end >  How to get request body in Spring Cloud Gateway and addHeader
How to get request body in Spring Cloud Gateway and addHeader

Time:01-17

I plan to migrate a project from Zuul to Spring Cloud Gateway. I have a "checksum code" and I don't know how to migrate it.

In the zuul code i get the url parameter and json body, I then do some checks.

HttpServletRequest request = requestContext.getRequest();
Map<String, String[]> parameterMap = getURLParamter(request);
String json = getBody(request);

if(securityCheck(parameterMap, json) == true) {
    requestContext.addZuulRequestHeader("check-success-then-next-filter", "1");
} else {
    requestContext.setResponseBody("{ msg:: check error }");
}

I have limited experience with Spring gateway please help me find what the equivalent code is in Spring Gateway,

CodePudding user response:

Spring Cloud gateway has filters to modify request body and response body.

ModifyResponseBody
ModifyRequestBody

As mentioned in the specs, for using these filters, we need to use DSL approach rather than YAML file for configuring routes. So essentially you will have a RouteBuilder like below -


    @Bean
    public RouteLocator myRoutes(RouteLocatorBuilder builder) {
    
    RouteLocatorBuilder.Builder routeLocator = builder.routes().route(
    p -> {
      p.method("POST").path("/<path>").filters(f->{
         f.modifyRequestBody(String.class, String.class,
                                    (exchange, reqMessage) -> {
                                        try {
                                            log.info(">>> INCOMING REQUEST <<< - {}", reqMessage);
                                            //Get query params
    exchange.getRequest().getQueryParams();
    //In case of any validation errors, throw an exception so that it can be handled by a global exception handler
                                            return Mono.just(reqMessage);
                                        } catch (Exception e) {
                                            log.error("Exception while modifying request body", e);
                                            throw new RuntimeException(e.getMessage());
                                        }
                                    });
      })
    }
    );
    
    }

A global exception handler could then send a standard response back -


    public class GlobalExceptionHandler extends AbstractErrorWebExceptionHandler {
       @Override
       protected RouterFunction<ServerResponse> getRoutingFunction (ErrorAttributes errorAttributes) {
       return RouterFunctions.route(RequestPredicates.all(),this::renderErrorResponse);
    }
    
    private Mono<ServerResponse> renderErrorResponse (ServletRequest request) {
      Map<String,Object> errorPropertiesMap = getErrorAttributes (request,ErrorAttributeOptions.defaults());
    String customErrMsg = errorPropertiesMap.get("message") != null ? errorPropertiesMap.get("message").toString() : null;
    
    if(customErrMsg != null) {
      return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(errorPropertiesMap.get("message")));
    } else {
    return ServerResponse.status(HttpStatus.BAD_REQUEST).contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(errorPropertiesMap));
    }
    }
    }

CodePudding user response:

GlobalFilter can be used to modify the request.

public class RequestTransaformationFilter implements GlobalFilter, Ordered {

  @Autowired 
  private ModifyRequestBodyGatewayFilterFactory modifyRequestBodyFilter;
  @Autowired 
  private RequestBodyRewrite requestBodyRewrite;

  @Override
  public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    return modifyRequestBodyFilter
        .apply(
            new ModifyRequestBodyGatewayFilterFactory.Config()
                .setRewriteFunction(String.class, String.class, requestBodyRewrite))
        .filter(exchange, chain);
  }

 
}

RequestRewrite bean can be configured to modify request:

public class RequestBodyRewrite implements RewriteFunction<String, String> {

  @Override
  public Publisher<String> apply(ServerWebExchange exchange, String body) {
    Gson gson = new Gson();
   
      Map<String, Object> map = gson.fromJson(body, Map.class);
      map.put("NewField", "value");
      return Mono.just(gson.toJson(map, Map.class));
    
  }
}
  •  Tags:  
  • Related