Create custome filter in spring boot
Posted By : Rahbar Ali | 28-May-2018
Sometimes we need to integrate some validation before request object goes to Controller.
So, in this case, we need Filter that invoke request object & perform some validation about data.
We are going to describe how to use a custom filter in spring boot.
The Spring give an interface Filter that has 3 methods. we will override both 3 methods because of its an interface.
1-
destroy
2-
doFilter - in this method we get request & response object of current request & perform our operation related to request object or (both) response
object.
3-
init
package com.pkg.checkrequest;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CheckRequest implements Filter{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
System.out.println("httpRequest.getLocalAddr() "+httpRequest.getLocalAddr());
filterChain.doFilter(httpRequest, httpResponse);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
In another class we will create bean of that class
package com.pkg.configuration;
import javax.servlet.Filter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.pkg.checkrequest.CheckRequest;
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean<Filter> userSecurityFilterRegistartion() {
CheckRequest checkRequest = new CheckRequest();
FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>(checkRequest);
registration.setFilter(checkRequest);
registration.addUrlPatterns("/api/v1/*");
return registration;
}
}
In the above example, we are intercepting an HTTP request object.
if we want to add some kind of validation like token varify or adding cookies or check cookies or perform any operations before request object goes to controller
then we can intercept httpRequest object like above & perform our custom validation on above object & then pass that object into controller if our condition is verified in filter
otherwise we will stop that request in filter & give validation message from filter.
Thanks.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rahbar Ali
Rahbar Ali is bright Java Developer and keen to learn new skills.