0

The POST request to /app/login should be redirected to http://abc:100/app/login and the DELETE reqeust to /app/login should be redirect to http://xyz:200/app/login. How can this be achieved in the Netflix Zuul route configuration ? Is there an option to mention the http method in the route configuration ?

Here is the entry that explains our scenario.

zuul:
  routes:
    Login:
      path: /app/login
      url: http://abc:100/app/login


    Logout:
      path: /app/login
      url: http://xyz:200/app/login
yathirigan
  • 5,619
  • 22
  • 66
  • 104

1 Answers1

2

UPDATE: spring-cloud-netflix won't do this out-of-the-box with easy configuration, but it's actually easy to add a custom ZuulFilter that does what you need. See How do you you create custom Zuul filters in Spring Cloud?

I think something like this should work for you:

@Component
public class MyCustomZuulFilter extends ZuulFilter {

    private urlPathHelper = new UrlPathHelper();

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        final String requestURI = 
            this.urlPathHelper.getPathWithinApplication(ctx.getRequest());

        if ("/app/login".equals(requestURI)
            && ctx.getRequest() != null
            && !StringUtils.isBlank(ctx.getRequest().getMethod())) {

            if ("DELETE".equals(ctx.getRequest().getMethod().toUpperCase())) {
                ctx.set("serviceId", "XYZ-SVC");
                ctx.setRouteHost(null);
                ctx.addOriginResponseHeader("X-Zuul-ServiceId", location);
            } else if ("POST".equals(ctx.getRequest().getMethod().toUpperCase())) {
                ctx.set("serviceId", "ABC-SVC");
                ctx.setRouteHost(null);
                ctx.addOriginResponseHeader("X-Zuul-ServiceId", location);
            }
        }
        return null;
    }
}

Note that you may still need a Zuul rule that maps /app/login to an existing service depending on your setup, as the bundled PreDecorationFilter automatically sets unrecognized routes as "forward.to".

I have exactly the same use case, but Zuul doesn't appear to be able to route based on HTTP method at the moment.

The code in question is in ProxyRouteLocator#getMatchingRoute method. It iterates through the defined ZuulRoute entries to find matching routes. The logic in #getMatchingRoute and the properties of ZuulRoute don't appear to match on HTTP method, unfortunately.

Community
  • 1
  • 1
BobG
  • 2,113
  • 17
  • 15