简体   繁体   English

在Zuul预过滤器中修改请求正文无效

[英]Modifying request body in Zuul pre filter not working

In my app I am using netflix zuul to route a request from a microservice (gateway) to another. 在我的应用程序中,我使用netflix zuul将请求从微服务(网关)路由到另一个。 The requests are being routed fine but I also want to introduce some parameters in the request body before it is routed to the appropriate microservice. 请求被很好地路由,但是我还想在将请求路由到适当的微服务之前在请求主体中引入一些参数。 For this I am using Zuul pre filter like this. 为此,我使用像这样的Zuul预过滤器。

public class SimpleFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(SimpleFilter.class);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 1;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {

        try {
            RequestContext context = RequestContext.getCurrentContext();

            InputStream in = (InputStream) context.get("requestEntity");
            if (in == null) {
                in = context.getRequest().getInputStream();
            }

            String body = StreamUtils.copyToString(in, Charset.forName("UTF-8"));

            // body = "request body modified via set('requestEntity'): "+ body;
            body = body.toUpperCase();
            context.set("requestEntity", new ByteArrayInputStream(body.getBytes("UTF-8")));

        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }

        return null;
    }

}

For now I am just trying to change the body to upper case but the microservice to which this request is routed doesn't receive the modified body (upper case). 现在,我只是尝试将主体更改为大写,但将此请求路由到的微服务未收到修改后的主体(大写)。 Instead it receives the original one. 相反,它接收原始的。 Am I doing something wrong. 难道我做错了什么。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks !! 谢谢 !!

试试这个吧,这可能在您的情况下有效。

requestContext.getCurrentContext().put("requestEntity", new ByteArrayInputStream(body.getBytes("UTF-8")));

Was able to do the following - transform a GET request to a POST request, and add body content to the (proxied) POST request. 能够执行以下操作-将GET请求转换为POST请求,并将正文内容添加到(代理的)POST请求中。

    public Object run() throws ZuulException {
        RequestContext context = RequestContext.getCurrentContext();
        context.addZuulRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        String body = String.format("a=%s&b=%s", a, b);

        final byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
        context.setRequest(new HttpServletRequestWrapper(context.getRequest()) {
            @Override
            public ServletInputStream getInputStream() {
                return new ServletInputStreamWrapper(bytes);
            }

            @Override
            public int getContentLength() {
                return bytes.length;
            }

            @Override
            public long getContentLengthLong() {
                return bytes.length;
            }

            @Override
            public String getMethod() {
                return "POST";
            }
        });

        return null;
    }

Turned out this method cannot change the request body within the requestContext. 原来,此方法无法更改requestContext中的请求正文。 Truly in the requestContext, a new field "requestEntity" is added, however, the request body from context.getRequest().getInputStream() remains the same after this operation. 确实在requestContext中添加了一个新字段“ requestEntity”,但是,执行此操作后,来自context.getRequest()。getInputStream()的请求主体保持不变。

You can modify the request body, see this answer for an example. 您可以修改请求正文,有关示例,请参见此答案 You just need to wrap the new request data and make sure you correctly report it's new content length. 您只需要包装新的请求数据,并确保正确报告新内容的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM