Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Adding Request Interceptor in Spring

DZone's Guide to

Adding Request Interceptor in Spring

· Integration Zone ·
Free Resource

The State of API Integration 2018: Get Cloud Elements’ report for the most comprehensive breakdown of the API integration industry’s past, present, and future.

In web applications/projects, we came across different scenarios where we need to check the request first before serving it or passing it to our main system or code.

For example in a MVC project we need to check every request that if it contains particular key/switch. That key/switch can inform us prior to processing that request to move it to particular system or portion e.g client side or admin side.

We came across the same case in which we need to verify the request first before passing it or mapping it to any controller class. For that purpose spring provides us with different listeners and hookups which can be used to overcome the cases like this.

For that we need to define a MVC request dispatcher in servlet-context.xml.

Servlet-Context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
 
    xsi:schemaLocation="http://www.springframework.org/schema/beans
 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 
 
http://www.springframework.org/schema/context
 
 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
 
 
http://www.springframework.org/schema/aop
 
 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
 
 
http://www.springframework.org/schema/mvc
 
 
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
<mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/application/list.htm/**/">   
            <bean class="com.javapitshop.interceptor.AuthenticationInterceptor">
        </bean></mvc:mapping></mvc:interceptor>
</mvc:interceptors>

we can define multiple interceptors depending upon our use. In above code AuthenticationInterceptor is the class which will be called upon first on every request. If this class returns true then that particular request will be processes otherwise that request will be dropped and next request is considered for processing.

Authentication Interceptor

public class AuthenticationInterceptor extends HandlerInterceptorAdapter {
 
    @Autowired
    private HttpSession session;
 
    /**
     * Instantiates a new authentication interceptor.
     */
    private AuthenticationInterceptor() {
        //
    }
 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        //Provide your implementation here.
    }
     
    @Override
    public boolean postHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        //Provide your implementation here.
    }
}

In above code of AuthenticationInterceptor we can provide implementation of both methods preHandle() and postHandle(). Method preHandle() is being executed before processing of request and postHandle() after processing of request. You can use it depending upon your use case.

Your API is not enough. Learn why (and how) leading SaaS providers are turning their products into platforms with API integration in the ebook, Build Platforms, Not Products from Cloud Elements.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}