I have downstream service that redirect to x.jsp this location is not in gateway route for example
gateway route --- localhost:8080/app - 192.168.1.1:80 (DownStream app)
in DownStream app when a user is not logged in, it redirects to 192.168.1.1:80/Login.jsp, which is in the Location
header of the response.
This URL is not using the gateway.
I want to write a zuul filter to change this redirect url by mapping in zuul routes dynamic for example for each url that routes in zuul filter change Location
header by the gateway. How can I do that?
You downstream app should respect the x-forwarded-* headers, and not generate redirects like that. But the following filter will change the redirect location for you anyway:
@Override
public String filterType() {
return "post";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
int status = RequestContext.getCurrentContext().getResponseStatusCode();
return status >= 300 && status < 400;
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
Map<String, String> requestHeaders = ctx.getZuulRequestHeaders();
Optional<Pair<String, String>> locationHeader = ctx.getZuulResponseHeaders()
.stream()
.filter(stringStringPair -> LOCATION.equals(stringStringPair.first()))
.findFirst();
if (locationHeader.isPresent()) {
String oldLocation = locationHeader.get().second();
String newLocation = ...
locationHeader.get().setSecond(newLocation);
}
return null;
}