Spring Cloud Zuul: apply filter only to a specific route

I use Spring Cloud Zuul to proxy some API requests to multiple external servers. The proxy server itself works well, but each service requires a (different) token specified in the request header.

I have successfully written a simple pre-filter for each token that applies the corresponding header. However, I now have a problem. Even after uploading the documentation, I cannot figure out how to make each filter apply only to the correct route. I do not want to perform url mapping as the URL changes in different environments. Ideally, I would have a way to get the route name in the filter.

My application.yml:

zuul:
  routes:
    foo:
      path: /foo/**
      url: https://fooserver.com
    bar:
      path: /bar/**
      url: https://barserver.com

Ideally, I would like to do something like this in FooFilter.java (prefilter):

public bool shouldFilter() {
    return RequestContext.getCurrentContext().getRouteName().equals("foo");
}

.

+4
1

proxy RequestContext, , . , serviceId. url direclty, , proxy. , , , proxy PreDecorationFilter, , , PreDecorationFilter ( 5 ).

@Override
public int filterOrder() {
    return 10;
}

@Override
public boolean shouldFilter() {
    RequestContext ctx = RequestContext.getCurrentContext();

    if ((ctx.get("proxy") != null) && ctx.get("proxy").equals("foo")) {
        return true;
    }
    return false;
}
+11

Source: https://habr.com/ru/post/1674333/


All Articles