Adding a new field to body of the request from Netflix Zuul Pre-filter

本小妞迷上赌 提交于 2019-12-11 07:57:55

问题


I'm trying to add a new field to request's body, in a Zuul Pre-filter.

I'm using one of the Neflix's Zuul sample projects from here, and my filter's implementation is very similar to UppercaseRequestEntityFilter from this sample.

I was able to apply a transformation such as uppercase, or even to completely modify the request, the only inconvenient is that I'm not able to modify the content of body's request that has a length more than the original length of the body's request.

This is my filter's implementation:

@Component
public class MyRequestEntityFilter extends ZuulFilter {
    public String filterType() {
        return "pre";
    }

    public int filterOrder() {
        return 10;
    }

    public boolean shouldFilter() {
        RequestContext context = getCurrentContext();
        return true;
    }

    public Object run() {
        try {
            RequestContext context = getCurrentContext();
            InputStream in = (InputStream) context.get("requestEntity");
            if (in == null) {
                in = context.getRequest().getInputStream();
            }

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

            body = body.replaceFirst("qqq", "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq");

            // body = body.toUpperCase();

            context.set("requestEntity", new ServletInputStreamWrapper(body.getBytes("UTF-8")));
        }
        catch (IOException e) {
            rethrowRuntimeException(e);
        }
        return null;
    }
} 

This is the request that I'm doing:

This is the response that I'm receiving:


回答1:


I was able to obtain what I wanted, using the implementation of PrefixRequestEntityFilter, from sample-zuul-examples:

@Component
public class MyRequestEntityFilter extends ZuulFilter {
    public String filterType() {
        return "pre";
    }

    public int filterOrder() {
        return 10;
    }

    public boolean shouldFilter() {
        RequestContext context = getCurrentContext();
        return true;
    }

    public Object run() {
        try {
            RequestContext context = getCurrentContext();
            InputStream in = (InputStream) context.get("requestEntity");
            if (in == null) {
                in = context.getRequest().getInputStream();
            }

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

            body = body.replaceFirst("qqq", "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq");

            byte[] bytes = body.getBytes("UTF-8");

            context.setRequest(new HttpServletRequestWrapper(getCurrentContext().getRequest()) {
                @Override
                public ServletInputStream getInputStream() throws IOException {
                    return new ServletInputStreamWrapper(bytes);
                }

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

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

        }
        catch (IOException e) {
            rethrowRuntimeException(e);
        }
        return null;
    }
}


来源:https://stackoverflow.com/questions/47811480/adding-a-new-field-to-body-of-the-request-from-netflix-zuul-pre-filter

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