Search code examples
javaspring-bootspring-cloudnetflix-zuulspring-cloud-netflix

ZuulFilter is not invoked


I use:

  • spring-boot 2.1.2.RELEASE
  • spring-cloud Greenwich.RELEASE

Zuul dependency:

implementation 'org.springframework.cloud:spring-cloud-starter-netflix-zuul'

I'm invoking a microservice from another one using zuul and load balanced rest template:

restTemplate.postForEntity("http://image/api/image", imageDto, ImageDto.class)

The request reaches the downstream service but the Authorization header (I tried with different ones) is not being set in the ZullFilter:

public class ZuulAuthFilter extends ZuulFilter {

    public static final String AUTHORIZATION_HEADER = "Authorization";

    public String filterType() {
        return PRE_TYPE;
    }

    public int filterOrder() {
        return PRE_DECORATION_FILTER_ORDER - 1;
    }

    public boolean shouldFilter() {
        return true;
    }

    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        String authorizationValue = ctx.getRequest().getHeaders(AUTHORIZATION_HEADER).nextElement();
        ctx.addZuulRequestHeader(AUTHORIZATION_HEADER, authorizationValue);
        return null;
    }
} 

I've also created a filter @Bean:

@Bean
public ZuulAuthFilter zuulAuthFilter() {
    return new ZuulAuthFilter();
}

Here is my zuul configuration:

zuul:
  sensitiveHeaders: Cookie,Set-Cookie
  ribbon:
    eager-load:
      enabled: true

It looks like the filter is not being invoked at all. I've added System.exit(0) to each overriden filter method and the app is still running.

I've annotated the Application class with @EnableZuulProxy

What am I missing? Why the filter might not be invoked? Should it be invoked in my case at all?


Solution

  • I'm invoking a microservice from another one using zuul proxy.

    According to the sources it seems that Zuul filters are invoked from ZuulServlet. Spring Cloud ZuulController only wraps ZuulServlet. So there is no way to invoke Zuul filter chain before calling restTemplate out of the box. And Zuul isn't intended for internal microservice communication.