What is HandlerInterceptor in Spring MVC

Chanaka MBK
Geek Culture
Published in
4 min readJan 18, 2021

In this post, I am going to describe the Spring MVC HandlerInterceptor, its usage, and behavior.

Overview

Before going to the HandlerInterceptor let’s discuss a bit about HandlerMapping.It is used to maps a method to a particular URL.So that the DispatcherServlet will be able to invoke it when processing a request. Also, the DispatcherServlet uses the HandlerAdapter to invoke the method.

Now we have a quick idea about the context and this is the time HandlerInterceptor comes into the play. It has three stages like actions before handling, after handling, and after completion (Rendering view object).

In the projects, we can use this interceptor for request validation, request authentication, avoid repetitive code, etc…

Please see the diagram to get a better idea of the process.

Interceptor work with Spring MVC

Dependencies

Spring dependency
Springboot dependency

Spring Handler Interceptor

Before going to the implementation let’s have a look at the content. It is an interface and it contains 4 main methods. Using these methods we can
do all kinds of pre and post-processing.

prehandle(): Called before the actual handler is executed, but the view is not generated yet.
postHandle() : Called after the handler is executed.
afterCompletion(): Called after the complete request has finished and the view was generated.
afterConcurrentHandlingStarted() : It started async execution, for type and/or instance examination. This is very rare in practical scenarios.

This is how preHandle() implementation will look like:

This intercepts the request before reaching the handler method. This method returns a boolean.

If true, it allows the request to continue in the execution chain for either another interceptor to the processor for the handler itself.

If false, spring assumes the interceptor has processed the request and our controller will never process this request. Interrupts any future execution in the execution chain including the actual handler.

This is how postHandle() implementation will look like:

This method placed in between the HandlerAdapter and before DispatcherServlet renders the view allowing the interceptor to inject additional attributes into ModelAndView objects. This method will be invoked on each interceptor in the chain in reverse order, so the first interceptor will be the last to be invoked.

This is how afterCompletion() implementation will look like:

This will be called after the request has completed processing. Normally this method may be used to perform some type of cleanup of resources.
This method will be invoked on each interceptor in the chain in reverse order, so the first interceptor will be the last to be invoked.

Implementation

There are two ways to do the implementation.
1) Implementing a HandlerInterceptor interface.
2) Extending HandlerInterceptorAdapter abstract class.

I‘m going to use the second approach. But the end of the day it will call HandlerInterceptor.Please see the diagram.

Configuration

There are 2 different ways to configure the interceptor.

For the Springboot project, we need to override the addInterceptors() method inside SpringMVCConfig class that implements WebMvcConfigurer.This SpringMVCConfig class is our custom implementation.

Spring class configueration

For the Spring we can use XML configuration in mvc-dispatcher-servlet.xml as given below. Using this we can define which controller request should go through the interceptor using <mvc:mapping> and <mvc:exclude-mapping> tags.

XML configueration
  1. If you want to config your controller class without any restriction, you can use the No:1 configuration.
  2. If you want to config a particular URI with the interceptor, you can use the No:2 configuration.
  3. If you want to include or exclude any specific URIS from the interceptor, you can use No:3,4 configurations.
  4. No:5 shows us the way we can configure our Interceptor class in the XML file.

I have completed 2 blog posts regarding authentication using the interceptor. Please visit the following links to see the practical usage of interceptors.

Basic authentication for Springboot REST API application with HandlerInterceptor

JWT authentication for Springboot REST API application with HandlerInterceptor

Hope this will be helpful to you and please leave a comment or message if you have any concerns.

--

--

Chanaka MBK
Geek Culture

Experienced Software Engineer with a demonstrated history of working in the information technology and services industry. Skilled in JAVA,Spring Boot, Angular.