Spring-cloud-gateway: Allow modification of the response body

Created on 21 Jun 2017  ·  10Comments  ·  Source: spring-cloud/spring-cloud-gateway

Hi,

I need to modify the body of both the request and the response (encryption/decryption).
As I see it, I can create a filter that sets a ServerHttpRequestDecorator in the exchange and override the getBody method, but I didn't find a way to do the same with the response. The WriteResponseFilter is the first filter and consequently the exchange used is the original exchange.
Would it be possible to add a ResponseBodyHandler interface to allow modifying the response body? Or did I overlook something?

Thanks in advance.

Most helpful comment

Hi,

I'll try to make a Gist to show you how I've done (I'll have to clean up corporate code first), but in short:

  • implement GlobalFilter & Ordered
  • important: the order has to be <-1 or else the standard NettyWriteResponseFilter will send the response before your filter gets a chance to be called
  • in the overriden filter method, create a ServerHttpResponseDecorator over exchange.getResponse(), override the writeWith method of this decorator and do your body modifications there (I had to cast the body from a Publisher to a Flux to make it easier to modify), return super.writeWith(yourModifiedBodyFlux). Mutate the exchange and set your decorator as the new response.

Hope it makes sense. I have 2 filters that work that way (the first one removes some fields from the JSON response, the second one ciphers the body of the response in AES).
Not the cleanest code I've written though, maybe there is a better way.

All 10 comments

Nevermind, I just added a GlobalFilter with an order of -2 and it works.

I'm trying, but no result...
Would you please post any sample code how to change response body?

Hi,

I'll try to make a Gist to show you how I've done (I'll have to clean up corporate code first), but in short:

  • implement GlobalFilter & Ordered
  • important: the order has to be <-1 or else the standard NettyWriteResponseFilter will send the response before your filter gets a chance to be called
  • in the overriden filter method, create a ServerHttpResponseDecorator over exchange.getResponse(), override the writeWith method of this decorator and do your body modifications there (I had to cast the body from a Publisher to a Flux to make it easier to modify), return super.writeWith(yourModifiedBodyFlux). Mutate the exchange and set your decorator as the new response.

Hope it makes sense. I have 2 filters that work that way (the first one removes some fields from the JSON response, the second one ciphers the body of the response in AES).
Not the cleanest code I've written though, maybe there is a better way.

Thanks a lot!
I'm new to reactive way, so I have to learn many things "in parallel mode" in short time...

And how do you cast the body from a Publisher to a Flux?

I got it "works" somehow, but always with exception "only one subscriber allowed". And that way, as I understand, is wrong.

response.bufferFactory().allocateBuffer().asInputStream() reproduces nothing...

I made a small example here : https://gist.github.com/WeirdBob/b25569d461f0f54444d2c0eab51f3c48
it just converts the body into to uppercase.
you can launch it and try going to localhost:8080/uuid

Thank you very much!!!
I got hope!
You've made my day!!!

We can simplify response body modification by using a plain GatewayFilter:

  1. Create GatewayFilter implementation with the same filter() content as in the sample above;
  2. Create method to return an instance of the filter, e.g., bodyToUppercase() (if you prefer - just for simplification);
  3. Add filter to the router like:
.route("routeIdHere")
    .predicate(path("/deep/deep/hurray"))
    ...
    .filter(bodyToUppercase(), NettyWriteResponseFilter.WRITE_RESPONSE_FILTER_ORDER - 1)
    ...
.uri("lb://serverId")

So, it can be achieved with a plain GatewayFilter, by forcing the order pass. Using the GlobalFilter is not mandatory, but using GatewayFilter we have clear picture of the routes.

How to append or modify the request body ? the length of the body may become longer, but the service behind the gateway received the request body have been truncated, some data lost.

I solved this problem by modifying the length of the head:
To modify the header CONTENT_LENGTH,You must to create a new HttpHeaders, and then copy the orignal values and remove the CONTENT_LENGTH, add the correct length(If you do not do this , the request body will be truncated if you modify the request body and the body become longer )

    HttpHeaders myHeaders = new HttpHeaders();
    copyMultiValueMap(request.getHeaders(), myHeaders);
    myHeaders.remove(HttpHeaders.CONTENT_LENGTH);
    myHeaders.set(HttpHeaders.CONTENT_LENGTH, String.valueOf(len));

    Flux<DataBuffer> bodyFlux = Flux.just(bodyDataBuffer);
    newRequest = new ServerHttpRequestDecorator(newRequest) {
        @Override
        public Flux<DataBuffer> getBody() { 
            return bodyFlux;
        }

        @Override
        public HttpHeaders getHeaders() {
            return myHeaders;               
        }
    };

private static void copyMultiValueMap(MultiValueMap source, MultiValueMap target) {
source.forEach((key, value) -> target.put(key, new LinkedList<>(value)));
}

Was this page helpful?
0 / 5 - 0 ratings