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?
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.
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.
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.
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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With