NetflixOSS Zuul过滤器拒绝请求 [英] NetflixOSS Zuul Filter for rejecting requests

查看:229
本文介绍了NetflixOSS Zuul过滤器拒绝请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在简单的spring-cloud-Netflix Api网关(反向代理)中使用ZuulFilter,以便针对自定义身份验证提供程序对请求进行身份验证(通过Rest调用).

I am trying to use a ZuulFilter in a simple spring-cloud-Netflix Api gateway (reverse proxy) in order to authenticate requests against a custom authentication provider (via Rest call).

过滤器应拒绝带有401的未经授权的请求,并且不要将这些请求进一步传递给代理服务.

The Filter should reject unauthorized requests with a 401 and don't pass those requests further down to the proxied services.

ZuulFilter甚至可能吗?我在Zuuls api中找不到文档,示例或其他内容.

Is that even possible for a ZuulFilter? I did not find documentation, example or something in Zuuls api.

有什么建议吗?

推荐答案

我开始使用它,进行了一些挖掘.确保您的请求尚未缓存.只需从ZuulFilter中的run()方法调用此方法即可.

I got this to work, took some digging. Make sure your request isn't cached already. Just call this method from your run() method inside your ZuulFilter.

/**
 * Reports an error message given a response body and code.
 * 
 * @param body
 * @param code
 */
private void setFailedRequest(String body, int code) {
    log.debug("Reporting error ({}): {}", code, body);
    RequestContext ctx = RequestContext.getCurrentContext();
    ctx.setResponseStatusCode(code);
    if (ctx.getResponseBody() == null) {
        ctx.setResponseBody(body);
        ctx.setSendZuulResponse(false);
    }
}

这篇关于NetflixOSS Zuul过滤器拒绝请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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