Spring Handlerinterceptor

By | | Updated : 2021-03-15 | Viewed : 11180 times

Spring Handlerinterceptor

Sometimes we need to preprocess or post proses the request based on requirement. So we have to intercept the request to pro/post process request. So here we can use the intercept concept. It is similar to servlet filter.

HandlerInterceptor Introduction

To Pre-Process/Post-Process for certain requests, we should use Interceptor to intercept request in Spring MVC. For Example, if we want to modify or check Principal User object before processing the request, we can manipulate/check Principal User.

Spring provides two type of interfaces to implement the request interception concept.

  • HandlerInterceptor.

  • HandlerInterceptorAdapter

Both above mentioned interface have same methods are

  • preHandle(..) is called before the actual handler is executed;

  • postHandle(..) is called after the handler is executed;

  • afterCompletion(..) is called after the complete request has finished.

HandlerInterceptorAdapter Vs HandlerInterceptor

In HandlerInterceptor, it will not allow to override the specific methods. Here we need to override the all the three methods. But in HandlerInterceptorAdapter, we can override specific methods. No need to override all the three methods.Please find the diagrammatic representation for interceptor concept.

Spring Handlerinterceptor

Excituion Flow:

  • Once request is received, the Dispatcher Servlet take help from HandlerMapping to get handler. HandlerMapping maps each request with Handler (Controller) and Interceptors

  • HandlerMapping returns HandlerExecutionChain which contains handler, interceptors and handler method's order.Dispatcher servlet will make use of HandlerAdapter to invoke all handler methods as per HandlerExecutionChain.

  • As per HandlerExecutionChain, preHandle() should invoke first. So HandlerAdapter will invoke preHandle().

  • After execution of Handler, HandlerAdapter will again call postHandle() of interceptor.

  • While sending the response to the view, again afterCompletion(..) will be invoked by HandlerAdapter.

Configuration of HandlerInterceptor

Xml configuration for HandlerInterceptor
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

			<mvc:mapping path="/getAdminHome" />
			<bean class="com.docsconsole.tutorials.springmvc5.interceptor.AdminRequestInterceptor" />
			<mvc:mapping path="/getUserHome" />
			<bean class="com.docsconsole.tutorials.springmvc5.interceptor.UserRequestInterceptor" />

Here we are providing two inner tags in <mvc:interceptor>.

  • 1. to map the request path which is to be intercept.

  • 2. to define the customized interceptor.

Java configuration for HandlerInterceptor
@ComponentScan(basePackages = { "com.docsconsole.tutorials.springmvc5"})
public class WebMvcConfig implements WebMvcConfigurer {
   AdminRequestInterceptor gdminRequestInterceptor() {
        return new AdminRequestInterceptor();
   UserRequestInterceptor userRequestInterceptor() {
       return new UserRequestInterceptor();
   public void addInterceptors(InterceptorRegistry registry) {

It is equivalent to previous xml configuration for interceptors.

Leave A Reply