Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Building Web Apps with Spring 5 and Angular

You're reading from   Building Web Apps with Spring 5 and Angular Modern end-to-end web application development

Arrow left icon
Product type Paperback
Published in Aug 2017
Publisher Packt
ISBN-13 9781787284661
Length 370 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ajitesh Kumar Shukla Ajitesh Kumar Shukla
Author Profile Icon Ajitesh Kumar Shukla
Ajitesh Kumar Shukla
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Introduction to Spring Web Framework 2. Preparing the Spring Web Development Environment FREE CHAPTER 3. Data Access Layer with Spring and Hibernate 4. Testing and Running Spring Web App 5. Securing Web App with Spring Security 6. Getting Started with Angular 7. Creating SPA with Angular and Spring 5 8. Unit Testing with Angular Apps 9. Securing an Angular App 10. Integrating Angular App with Spring Web APIs 11. Deploying the Web Application

Handling Interceptors

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 the preHandle method gets executed before the controller method is invoked
  • postHandle: The code within the postHandle method is executed after the controller method is invoked
  • afterCompletion: The code within afterCompletion 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 the HandlerInterceptorAdapter class. In the following example, only the preHandle method is implemented. Other methods such as postHandle and afterCompletion 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 extending WebMvcConfigurerAdapter, thereby adding the interceptor with appropriate path patterns within the addInterceptors 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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime