Background
I want to implement the design presented in this article.
It can be summarised by the diagram below:
What I have so far
I have most of that done using:
I have also written a Zuul PRE filter that checks for an Access Token, contacts the IDP and create a JWT. The JWT is then added to the header for the request forwarded to the downstream service.
Problem
Now my question is quite specific to Zuul and its filters. If authentication fails in the API gateway for any reason, how can I can stop the routing and respond directly with a 401 without continuing the filter chain and forwarding the call?
At the moment if authentication fails the filter won't add the JWT to the header and the 401 will come from the downstream service. I was hoping my gateway could prevent this unnecessary call.
I tried to see how I could use com.netflix.zuul.context.RequestContext
to do this but the documentation is quite poor and I couldn't find a way.
Zuul is a gateway service that provides dynamic routing, monitoring, resiliency, security, and more.
Zuul 1 and Archaius 1 have both been superseded by later versions that are not backward compatible. The following Spring Cloud Netflix modules and corresponding starters will be placed into maintenance mode: spring-cloud-netflix-archaius. spring-cloud-netflix-hystrix-contract.
Zuul is built to enable dynamic routing, monitoring, resiliency, and security. It can also route the requests to multiple Amazon Auto Scaling Groups. For Example, /api/products are mapped to the product service and /api/user is mapped to the user service.
For example, / may be mapped to your web application, /api/users is mapped to the user service and /api/shop is mapped to the shop service. Zuul is a JVM-based router and server-side load balancer from Netflix. Netflix uses Zuul for the following: Authentication.
You could try setting setSendZuulResponse(false)
in the current context. This should not route the request. You could also call removeRouteHost()
from the context, which would achieve the same. You could usesetResponseStatusCode
to set the 401 status code.
Add the following within your run method, it will solve this problem
ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401);
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