How to add some data in body of response for Cloud Api Gateway

霸气de小男生 提交于 2019-12-30 09:49:59

问题


I'm adding some auth logic into cloud api gateway. I've added GatewayFilter:

import java.util.List;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.util.PatternMatchUtils;
import org.springframework.web.server.ServerWebExchange;

import reactor.core.publisher.Mono;

public class AuthorizationFilter implements GatewayFilter {
  @Override
  public Mono<Void> filter(
    ServerWebExchange exchange, GatewayFilterChain chain) {
    List<String> authorization = exchange.getRequest().getHeaders().get("Authorization");
    if (CollectionUtils.isEmpty(authorization) &&
      !PatternMatchUtils.simpleMatch(URL_WITHOUT_AUTH, exchange.getRequest().getURI().toString())) {
      exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
      //Add some custom data in body of the response
      return exchange.getResponse().setComplete();
    }
    String token = authorization.get(0).split(" ")[1];
    // token validation
    return chain.filter(exchange);
  }
}

but I can't add some data into the body of response. Can you please help me to find out how it works and how I can customize that?

P.S. I'm trying to add some data in response using flux but it doesn't work:

 DataBuffer b = exchange.getResponse().bufferFactory().allocateBuffer(256);
      b.write("12345".getBytes());
      return exchange.getResponse().writeWith(s -> Flux.just(b));

What I'm doing wrong?


回答1:


After some help from spring guys, I was able to make it work. So instead of writing directly to response I had to throw my custom exception and handle it properly:

@Bean
public ErrorWebExceptionHandler myExceptionHandler() {
  return new MyWebExceptionHandler();
}

public class MyWebExceptionHandler implements ErrorWebExceptionHandler {
  @Override
  public Mono<Void> handle(
    ServerWebExchange exchange, Throwable ex) {
    byte[] bytes = "Some text".getBytes(StandardCharsets.UTF_8);
    DataBuffer buffer = exchange.getResponse().bufferFactory().wrap(bytes);
    return exchange.getResponse().writeWith(Flux.just(buffer));
  }
}



回答2:


You should use ServerHttpResponseDecorator to modify the response.

Your code should be like:

import java.util.List;

import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.util.PatternMatchUtils;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import reactor.core.publisher.Mono;

public class AuthorizationFilter implements GatewayFilter {
  @Override
  public Mono<Void> filter(
    ServerWebExchange exchange, GatewayFilterChain chain) {
    List<String> authorization = exchange.getRequest().getHeaders().get("Authorization");
    if (CollectionUtils.isEmpty(authorization) &&
      !PatternMatchUtils.simpleMatch(URL_WITHOUT_AUTH, exchange.getRequest().getURI().toString())) {
      exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);

      ServerHttpResponse originalResponse = exchange.getResponse();
      DataBufferFactory bufferFactory = originalResponse.bufferFactory();
      ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
        @Override
        public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
            if (body instanceof Flux) {
                Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
                return super.writeWith(fluxBody.map(dataBuffer -> {
                    // probably should reuse buffers 
                    byte[] content = new byte[dataBuffer.readableByteCount()];
                    dataBuffer.read(content);
                    byte[] uppedContent = new String(content, Charset.forName("UTF-8")).toUpperCase().getBytes();
                    return bufferFactory.wrap(uppedContent);
                }));
            }
            return super.writeWith(body); // if body is not a flux. never got there.
        }           
      };
      return chain.filter(exchange.mutate().response(decoratedResponse).build()); // replace response with decorator
    }
    String token = authorization.get(0).split(" ")[1];
    // token validation
    return chain.filter(exchange);
  }
}

You can find a complete example here.



来源:https://stackoverflow.com/questions/48491098/how-to-add-some-data-in-body-of-response-for-cloud-api-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!