In this section, we will learn about some of the following:
- Why use interceptors?
- How to implement interceptor methods such as pre-handle and post-handle to process the web requests before and after the requests get processed by the controllers respectively.
Interceptors' implementation provides methods for intercepting incoming requests before it gets processed by the controller classes or, intercepting the outgoing response after being processed by the controller and before being fed to the client. Interceptor methods help to get away with boilerplate code which is required to be invoked on each request and response. For example, let's take the authentication scenario where every request need to be checked for an authenticated session before being processed by the code in the controller. If the session is not found to be authenticated, the request is forwarded to the login page, else the request is forwarded to the controller for further processing. Given that the controller logic could span across multiple controller classes, and the aforementioned authentication logic needs to be processed before one or more controllers processes the incoming requests, it is suitable to put the authentication processing in the interceptor method. Another example of using interceptor methods includes measuring the time spent in the method execution.
For implementing interceptors for processing web requests-response, custom interceptor classes need to be implemented. Custom interceptor classes need to implement one or all of the methods provided in the HandlerInterceptor
interface which are as follows:
preHandle
: The code within thepreHandle
method gets executed before the controller method is invokedpostHandle
: The code within thepostHandle
method is executed after the controller method is invokedafterCompletion
: The code withinafterCompletion
is executed after the view gets rendered
In the following example, we will use an interceptor to process web requests arriving from the signup form. The following steps are required to be taken:
- Create an Interceptor class: Create an
Interceptor
class extending theHandlerInterceptorAdapter
class. In the following example, only thepreHandle
method is implemented. Other methods such aspostHandle
andafterCompletion
are not provided with the implementation:
public class SignupInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String emailAddress = request.getParameter("emailaddress"); String password = request.getParameter("password"); if(StringUtils.isEmpty(emailAddress) || StringUtils.containsWhitespace(emailAddress) || StringUtils.isEmpty(password) || StringUtils.containsWhitespace(password)) { throw new Exception("Invalid Email Address or Password. Please try again."); } return true; } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception { ... } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { ... } }
- Implement the Configuration class: Implement a custom
@Configuration
class by extendingWebMvcConfigurerAdapter
, thereby adding the interceptor with appropriate path patterns within theaddInterceptors
method. If the path pattern is not specified, the interceptor is executed for all the requests.@Configuration public class AppConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new SignupInterceptor()).addPathPatterns("/account/signup/process"); } }
- Make sure that the controller with the appropriate URI pattern is implemented as specified within the
addInterceptors
method in the aforementioned@Configuration
class.@Controller @RequestMapping("/account/*") public class UserAccountController { @RequestMapping("/signup") public String signup() { return "signup"; } @RequestMapping("/signup/process") public String processSignup(ModelMap model, @RequestParam("nickname") String nickname, @RequestParam("emailaddress") String emailAddress, @RequestParam("password") String password) { model.addAttribute("login", true); model.addAttribute("nickname", nickname); return "index"; } }
In the next section, you will learn about the different types of responses from controllers.