Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NetflixOSS Zuul Filter for rejecting requests

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).

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

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

Any suggestions?

like image 391
Thomas Jäckle Avatar asked Jun 28 '15 11:06

Thomas Jäckle


People also ask

What is the use of Zuul filter?

Zuul uses a range of different types of filters that enable us to quickly and nimbly apply functionality to our edge service. These filters help us perform the following functions: Authentication and Security — identifying authentication requirements for each resource and rejecting requests that do not satisfy them.

Is Netflix still using Zuul?

The new version of the Zuul gateway is built on top of the Netty server, and includes some improvements and new features. You can read more about them on the Netflix blog. Despite this decision being made by the Netflix cloud team, the Spring Cloud team has abandoned development of the Zuul module.

Is Zuul a reverse proxy?

Zuul is the library used to provide the reverse proxy, based on the route it will forward to configure URL or service-id by passing the necessary information.

What is the difference between Zuul and spring cloud gateway?

Zuul is built on servlet 2.5 (works with 3. x), using blocking APIs. It doesn't support any long lived connections, like websockets. Gateway is built on Spring Framework 5, Project Reactor and Spring Boot 2 using non-blocking APIs.


1 Answers

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);
    }
}
like image 159
abeauchamp Avatar answered Sep 22 '22 20:09

abeauchamp