Spring Cloud: Adding Filters in Zuul Gateway

This tutorial is the continuation of Spring Cloud: Exploring Zuul Gateway tutorial. In this tutorial, we will be exploring the functionality of filters provided by Zuul.

As discussed earlier Zuul provides various filters which we can use for request validation or processing. Let’s say if you have an incoming request and if you want to check whether the user is authenticated or not, you can use Zuul pre-filter for this.  If your request is processed and if you want to encrypt the response you can use Zuul post filter. The major upside of Zuul filter is, you can manage all of your filters at a centralized location.

Zuul has provision to create following four types of filter.

  1. pre filters run before the request is routed.
  2. route filters can handle the actual routing of the request.
  3. post filters run after the request has been routed.
  4. error filters run if an error occurs while handling the request.

Let’s add those filters in our project. For that, we will be using the project created in our previous tutorial, Spring Cloud: Exploring Zuul Gateway.

To add those filters in our project you will need to create classes which extend ZuulFilter class. In this case, since we are exploring all of those filters, we will create one class for each type of filter.

TL;DR You can download whole project by clicking following link.


This is a Maven Project. Our project structure is given below. We will be adding four filter related files and will be updating our configuration file which are as follows:

  1. AsmZuulApplication.java ā€“ Spring boot runā€“able file
  2. ErrorFilter.java – For error filter
  3. PostFilter.java – For post filter
  4. PreFilter.java – For pre filter
  5. RouteFilter.java – For route filter
Project Structure

Configuring Error Filter

Create file ErrorFilter.java and add the following content. Make sure that filterType() function return “error” to make this filter as Error filter.

package com.example.asmzuul;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
public class ErrorFilter extends ZuulFilter {
	@Override
	public boolean shouldFilter() {
		return true;
	}
	@Override
	public Object run() throws ZuulException {
		System.out.println("Invoking Error Filter");
		return null;
	}
	@Override
	public String filterType() {
		return "error";
	}
	@Override
	public int filterOrder() {
		return -1;
	}
}

Configuring Post Filter

Create file PostFilter.java and add the following content. Make sure that filterType() function return “post” to make this filter as Post filter. In case if you have multiple post-filters you can define there order by using filterOrder() function.

package com.example.asmzuul;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
public class PostFilter extends ZuulFilter {
	@Override
	public boolean shouldFilter() {
		return true;
	}
	@Override
	public Object run() throws ZuulException {
		System.out.println("Invoking Post Filter");
		return null;
	}
	@Override
	public String filterType() {
		return "post";
	}
	@Override
	public int filterOrder() {
		return 0;
	}
}

Configuring Pre Filter

Create file PreFilter.java and add the following content. Make sure that filterType() function return “pre” to make this filter as Pre-filter. In case if you have multiple filters you can define there order by using filterOrder() function.

package com.example.asmzuul;
import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
public class PreFilter extends ZuulFilter {
	@Override
	public boolean shouldFilter() {
		return true;
	}
	@Override
	public Object run() throws ZuulException {
		RequestContext ctx = RequestContext.getCurrentContext();
		HttpServletRequest request = ctx.getRequest();
		System.out.println("Type Of Request : " + request.getMethod());
		System.out.println("URL : " + request.getRequestURL().toString());
		return null;
	}
	@Override
	public String filterType() {
		return "pre";
	}
	@Override
	public int filterOrder() {
		return 0;
	}
}

Just for an example, in this filter, we have printed HTTP Method of incoming request and URL of the endpoint which is being invoked.


Configuring Route Filter

Create file RouteFilter.java and add the following content. Make sure that filterType() function return “route” to make this filter as Route filter.

package com.example.asmzuul;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
public class RouteFilter extends ZuulFilter {
	@Override
	public boolean shouldFilter() {
		return true;
	}
	@Override
	public Object run() throws ZuulException {
		System.out.println("Invoking Route Filter");
		return null;
	}
	@Override
	public String filterType() {
		return "route";
	}
	@Override
	public int filterOrder() {
		return 0;
	}
}

Updating our Configuration

Now we will need to create beans of those filter classes in our configuration class. So navigate to AsmZuulApplication.java and create beans of those filters as shown below.

package com.example.asmzuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
@EnableDiscoveryClient
@EnableZuulProxy
@SpringBootApplication
public class AsmZuulApplication {
	public static void main(String[] args) {
		SpringApplication.run(AsmZuulApplication.class, args);
	}

	@Bean
	public PreFilter preFilter() {
		return new PreFilter();
	}

	@Bean
	public PostFilter postFilter() {
		return new PostFilter();
	}

	@Bean
	public ErrorFilter errorFilter() {
		return new ErrorFilter();
	}

	@Bean
	public RouteFilter routeFilter() {
		return new RouteFilter();
	}
}

That’s all for configuration…


Testing it…

Let’s see the working of those filters in action. For that, we will start Eureka server first. Then we will start our producer project and finally, we will launch our Zuul project. Now we will try to access one of The endpoints of the producer.

After accessing the endpoint check console output of Zuul project. you should see those filters getting invoked as shown in below screenshot. If you have noticed then the order of filter execution is Pre-filter -> Route Filter -> Post Filter. Along with that, you can clearly see that our Pre-filter has printed HTTP method and endpoint URL of the request.

Output

Downloads:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.