Book Image

Building Web Apps with Spring 5 and Angular

By : Ajitesh Kumar Shukla
Book Image

Building Web Apps with Spring 5 and Angular

By: Ajitesh Kumar Shukla

Overview of this book

Spring is the most popular application development framework being adopted by millions of developers around the world to create high performing, easily testable, reusable code. Its lightweight nature and extensibility helps you write robust and highly-scalable server-side web applications. Coupled with the power and efficiency of Angular, creating web applications has never been easier. If you want build end-to-end modern web application using Spring and Angular, then this book is for you. The book directly heads to show you how to create the backend with Spring, showing you how to configure the Spring MVC and handle Web requests. It will take you through the key aspects such as building REST API endpoints, using Hibernate, working with Junit 5 etc. Once you have secured and tested the backend, we will go ahead and start working on the front end with Angular. You will learn about fundamentals of Angular and Typescript and create an SPA using components, routing etc. Finally, you will see how to integrate both the applications with REST protocol and deploy the application using tools such as Jenkins and Docker.
Table of Contents (18 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

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.