如何将修改/包装的HTTPServletRequest传递给后续的Zuul过滤器? [英] How to pass modified/wrapped HTTPServletRequest to subsequent Zuul Filters?

查看:686
本文介绍了如何将修改/包装的HTTPServletRequest传递给后续的Zuul过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个Zuul Pre-Filter(Filter1),它将检查传入的HTTPServletRequest,并对其中的查询参数进行一些更改,以将其嵌入到自定义创建的请求中(包装HttpServletRequestWrapper)。

现在,我想将这个自定义包装的请求传递给下一个Zuul预过滤器(Filter2)。我怎样才能做到这一点?



在使用常规的Servlet过滤器之前,我们在Filter1中使用以下代码:

  chain.doFilter(new CustomRequestWrapper(request,extra),response);但是,在Zuul过滤器中,Zuul框架负责调用后续的过滤器,并将请求/请求传递给/响应对象。如何重写?

解决方案

在Filter1执行结束之前,请调用以下内容来覆盖现有的HTTPServletRequest定制请求

  RequestContext context = RequestContext.getCurrentContext(); 
HttpServletRequest request = context.getRequest();



context.setRequest(new CustomRequestWrapper(request,extra));

参考 http:/ /netflix.github.io/zuul/javadoc/zuul-core/com/netflix/zuul/context/RequestContext.html#setRequest(javax.servlet.http.HttpServletRequest)


We have a Zuul Pre-Filter (Filter1) which will inspect the incoming HTTPServletRequest and make some changes to the query parameters in it to embed it to a custom created request (wrapping HttpServletRequestWrapper).

Now, i want to pass this custom wrapped request to the next Zuul Pre-Filter (Filter2). How can i do that ?

Earlier when using regular Servlet Filters, we use as following in my Filter1

chain.doFilter(new CustomRequestWrapper(request, extra), response);

but, in Zuul Filter, the Zuul framework takes care of invoking the subsequent filters and passing the request/response objects. How can i override that ?

解决方案

Before the Filter1 execution ends, call the following to over-write the existing HTTPServletRequest with our wrapped custom request

RequestContext context = RequestContext.getCurrentContext();
HttpServletRequest request = context.getRequest();
.
.
. 
context.setRequest(new CustomRequestWrapper(request, extra));

Reference: http://netflix.github.io/zuul/javadoc/zuul-core/com/netflix/zuul/context/RequestContext.html#setRequest(javax.servlet.http.HttpServletRequest)

这篇关于如何将修改/包装的HTTPServletRequest传递给后续的Zuul过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆