问题描述
我想添加一个自定义错误Zuul过滤器,并希望确保不执行SendErrorFilter.我看过一些github链接,包括 Spring-cloud/spring-cloud- netflix 和各种堆栈溢出问题:-
I want to add a custom error Zuul Filter and want to make sure SendErrorFilter does not execute. I have looked at few github links including Spring-cloud/spring-cloud-netflix and various stack-overflow questions:-
我的代码如下-
public class CustomErrorFilter extends ZuulFilter {
private static final Logger LOG = LoggerFactory.getLogger(CustomErrorFilter.class);
@Override
public String filterType() {
return "post";
}
@Override
public int filterOrder() {
return -1;
}
@Override
public boolean shouldFilter() {
RequestContext ctx=RequestContext.getCurrentContext();
if(ctx.getThrowable()!=null)
return true;
else
return false;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
ctx.setThrowable(null); // response is not returned unless
throwable is set to null.
ctx.remove("error.status_code");
ctx.setResponseBody("Error");
ctx.getResponse().setContentType("text/plain");
ctx.setResponseStatusCode(400);
}
return null;
}
我遇到以下问题-
-
将过滤器顺序设置为
-1
不会禁止sendErrorFilter
运行.
要停止运行sendErrorFilter,我需要设置bootstrap.yml
To stop sendErrorFilter from running, I need to setzuul.SendErrorFilter.error.disable=true
in bootstrap.yml
要获取在自定义错误过滤器中设置的响应正文,我需要将throwable
设置为null
,如 github .
To get a response body which is set in the custom error filter, i need to set throwable
to null
as mentioned in the github.
将过滤器设置为错误" 类型不会执行任何操作,并且自定义文件管理器不会运行.
Setting a filter as type "error" does nothing, and the custom filer does not run.
我想解释一下,我做错了什么,以及处理自定义错误过滤器的最正确方法是什么,因为网络上有很多相互矛盾的信息.
I would like someone to explain, what I am doing wrong and what is the most correct way of handling custom error filters, because there is lot of conflicting information available on the web.
依赖项-
-
春天的云-Edgware.RELEASE
spring cloud - Edgware.RELEASE
spring cloud netflix启动器zuul-1.4.3发布
spring cloud netflix starter zuul- 1.4.3.RELEASE
推荐答案
filtertype()
如果要处理错误情况,应返回"error"
filtertype()
should return "error"
if you want to handle error scenario
filterOrder()
应该是-1
,才能在SendErrorFilter
将以下行添加到您的过滤器(CustomErrorFilter)
Add the following lines to your filter(CustomErrorFilter )
protected static final String SEND_ERROR_FILTER_RAN = "sendErrorFilter.ran";
和
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
ctx.set(SEND_ERROR_FILTER_RAN);
// rest of your code
return null;
}
ctx.set(SEND_ERROR_FILTER_RAN);
将阻止SendErrorFilter
运行.
更新:
在shouldFilter()方法org/springframework/cloud/netflix/zuul/filters/post/SendErrorFilter.java"rel =" noreferrer> SendErrorFilter
Check shouldFilter()
method in SendErrorFilter
@Override
public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
// only forward to errorPath if it hasn't been forwarded to already
return ctx.getThrowable() != null
&& !ctx.getBoolean(SEND_ERROR_FILTER_RAN, false);
}
对于每个请求,都会创建一个RequestContext
,但是spring没有在上下文中设置SEND_ERROR_FILTER_RAN
.可能是对于较旧的版本,您必须从yaml文件(zuul.SendErrorFilter.error.disable=true
)配置为较新的版本(1.4.3.RELEASE
),其代码例如ctx.set(SEND_ERROR_FILTER_RAN)
.
For every request a RequestContext
gets created, but spring is not setting SEND_ERROR_FILTER_RAN
in the context. May be for older version you have to configure from yaml file (zuul.SendErrorFilter.error.disable=true
) for newer version(1.4.3.RELEASE
) its from code like ctx.set(SEND_ERROR_FILTER_RAN)
.
默认情况下,!ctx.getBoolean(SEND_ERROR_FILTER_RAN, false)
的计算结果为true,并且将执行SendErrorFilter
的run()
方法.
By default !ctx.getBoolean(SEND_ERROR_FILTER_RAN, false)
this will evaluates to true and run()
method of SendErrorFilter
will execute.
如果将ctx.set(SEND_ERROR_FILTER_RAN)
放入CustomErrorFilter
中且filterOrder()
为-1,则将首先执行CustomErrorFilter
,我们将RequestContext
设置为SEND_ERROR_FILTER_RAN
.现在,当它转到SendErrorFilter
的shouldFilter()
方法时,计算结果为false,并且不会执行SendErrorFilter
的run()
方法.
if you put ctx.set(SEND_ERROR_FILTER_RAN)
in your CustomErrorFilter
with filterOrder()
as -1, your CustomErrorFilter
will execute first and we are setting the RequestContext
with SEND_ERROR_FILTER_RAN
as true. Now when it goes to shouldFilter()
method of SendErrorFilter
evaluate to false and it won't execute run()
method of SendErrorFilter
.
这篇关于Spring Cloud Zuul-自定义错误过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!