How to use filters in spring boot – Spring Boot allows to add a filter that intercepts and filters the request and response content. In the Spring Boot application, there are options for registering custom filters. Spring Boot uses the filter to perform some request processing before delivering the request to the controller. Spring boot filters can be used by implementing the Filter interface and extending the HTTPFilter class.
Spring Boot filters intercept and filter requests and responses, as well as preprocess data before passing it on to the spring boot controller classes. The request and response can both be modified by the filter before being sent to the controller and the client application. Filters can prevent requests from being delivered to the controller class.
In this post, we’ll look at how to use filters in Spring Boot and what configuration options are available.
How to use filters in spring boot
Various approaches can be used to create filters in the spring boot application. In a spring boot application, filters are used to filter requests and responses before forwarding them to the controller class. Filters can be created in a spring boot application by implementing an interface or extending a predefined class.
1. Using Filter Interface
The Filter interface in the spring boot application can be used to create spring boot filters. Init, destroy, and doFilter are the three methods in the Filter interface. When the client application sends a request, the doFilter method is called before the request is passed to the controller class.
MyFilter.java
package com.yawintutor;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.stereotype.Component;
@Component
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain)
throws IOException, ServletException {
System.out.println("START doFilter");
filterchain.doFilter(request, response);
System.out.println("END doFilter");
}
}
TestController.java
package com.yawintutor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping(value = "/test", method = RequestMethod.GET)
public String testFilter() {
System.out.println("Executing testFilter Method");
return "TEST OK";
}
}
Output
START doFilter
Executing testFilter Method
END doFilter
2. Using HttpFilter class
A spring boot filter can be created by extending the HttpFilter class. The HttpFilter class contains the default implementation of the Filter interface. The filter can be implemented by overriding the doFilter method from the HttpFilter class. The custom filter should be annotated using the @Component annotation.
MyFilter.java
package com.yawintutor;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpFilter;
import org.springframework.stereotype.Component;
@Component
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain)
throws IOException, ServletException {
System.out.println("START doFilter");
filterchain.doFilter(request, response);
System.out.println("END doFilter");
}
}
3. Using Order in multiple filter
You can create multiple filters with the spring boot application. Filters for spring boot can be made by implementing the Filter interface or by extending the HttpFilter class. The @Component annotation should be added in the filter class in the spring boot application.
The @Order annotation can be used to describe how the multiple filters should be executed. The filter will be invoked by the @Order annotation’s parameter.
MyFilter1.java
package com.yawintutor;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(1)
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain)
throws IOException, ServletException {
System.out.println("START doFilter1");
filterchain.doFilter(request, response);
System.out.println("END doFilter1");
}
}
MyFilter2.java
package com.yawintutor;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(2)
public class MyFilter2 implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain)
throws IOException, ServletException {
System.out.println("START doFilter2");
filterchain.doFilter(request, response);
System.out.println("END doFilter2");
}
}
Output
START doFilter1
START doFilter2
Executing testFilter Method
END doFilter2
END doFilter1
4. Using URL pattern with @WebFilter
The @WebFilter annotation in Spring Boot allows you to define filters. The filter can be configured to only run if the request url pattern matches the filter’s configured url pattern. A single filter can have one or more url patterns. For different url patterns, several filters can be specified.
If the request url matches the url pattern supplied in the filter, the doFilter method will be executed for the request before it is passed to the controller class in the spring boot application. The doFilter method will not be called if the request url does not match the url pattern supplied in the filter.
MyURLFilter.java
package com.yawintutor;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
@WebFilter({"/test","/hello/*"} )
public class MyURLFilter implements Filter{
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterchain)
throws IOException, ServletException {
System.out.println("START doFilter");
filterchain.doFilter(request, response);
System.out.println("END doFilter");
}
}
If you use the @WebFilter annotation in a spring boot application, you must also use the @ServletComponentScan annotation to scan all servlet components like filter, servlet, and so on. The @ServletComponentScan annotation should be applied to the main class of the spring boot application. Before starting the spring boot application, all servlet-related classes should be scanned using the annotation @ServletComponentScan.
SpringBootFilterApplication.java
package com.yawintutor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
@SpringBootApplication
@ServletComponentScan
public class SpringBootFilterApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootFilterApplication.class, args);
}
}
Output
START doFilter
Executing testFilter Method
END doFilter