Netflix的Zuul错误过滤器实现

时间:2019-04-30 08:21:27

标签: java spring-boot netflix-zuul spring-cloud-netflix

netflix的zuul网关服务的错误过滤器的示例实现是什么?我发现的所有示例都不正确,或者过时了。

1 个答案:

答案 0 :(得分:0)

您可以创建自己的自定义过滤器,并将该过滤器直接在Zuul的默认SendErrorFilter之前执行,但是有时默认过滤器会覆盖您的正文或HTTP错误。 我更喜欢通过将默认过滤器放在应用程序属性中来禁用它:

zuul.SendErrorFilter.error.disable=true

然后通过扩展默认值创建自己的CustomSendErrorFilter。 一个示例实现为:

@Component
public class SendErrorCustomFilter extends SendErrorFilter {

    private static final Logger LOG = LoggerFactory.getLogger(SendErrorCustomFilter.class);
    private static final String SERVLET_ERROR_STATUS_CODE = "javax.servlet.error.status_code";
    private static final String SERVLET_ERROR_EXCEPTION = "javax.servlet.error.exception";
    private static final String SERVLET_ERROR_MESSAGE = "javax.servlet.error.message";

    @Value("${error.path:/error}")
    private String errorPath;

    @Override
    public Object run() {
        try {
            RequestContext ctx = RequestContext.getCurrentContext();
            ExceptionHolder exception = findZuulException(ctx.getThrowable());
            HttpServletRequest request = ctx.getRequest();
            Throwable cause = exception.getThrowable().getCause();
            int statusCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;

            if (causeIsIOError(cause)) {
                statusCode = HttpServletResponse.SC_CONFLICT;
            } else if (causeIsAuthorizationError(cause)) {
                statusCode = HttpServletResponse.SC_UNAUTHORIZED;
            }

            request.setAttribute(SERVLET_ERROR_STATUS_CODE, statusCode);

            LOG.warn("Error during filtering", cause);
            request.setAttribute(SERVLET_ERROR_EXCEPTION, cause);

            if (StringUtils.hasText(exception.getErrorCause())) {
                request.setAttribute(SERVLET_ERROR_MESSAGE, cause.getMessage());
            }

            RequestDispatcher dispatcher = request.getRequestDispatcher(this.errorPath);
            if (dispatcher != null) {
                ctx.set(SEND_ERROR_FILTER_RAN, true);
                if (!ctx.getResponse().isCommitted()) {
                    ctx.setResponseStatusCode(exception.getStatusCode());
                    dispatcher.forward(request, ctx.getResponse());
                }
            }

        } catch (Exception ex) {
            ReflectionUtils.rethrowRuntimeException(ex);
        }
        return null;
    }

    private boolean causeIsIOError(Throwable cause) {
        return cause instanceof InvalidTokenPayloadException
                || cause instanceof InvalidResponseBodyException;
    }

    public boolean causeIsAuthorizationError(Throwable cause) {
        return cause instanceof InvalidJWTTokenException ||
                cause instanceof NoPermissionForResourceException ||
                cause instanceof MissingAuthorizationHeaderException;
    }

这样,您可以控制将要发送回客户端的错误。我提取了一些方法来检查不同种类的异常,并根据这些异常放置不同的HTTP错误。 我正在使用导致异常的原因,因为这些异常都包含在ZuulException中。