💻

Creating and Using Interceptors in Spring Boot

Jul 1, 2024

Creating and Using Interceptors in Spring Boot

Introduction

  • Instructor: Nilan from The Tech Mojo
  • Topic: Creating and using interceptors in Spring Boot
  • Video Structure:
    • Chapter 1: Introduction to Spring Interceptors
    • Chapter 2: Differences between Spring Interceptors and Servlet Filters
    • Chapter 3: Implementing and registering an Interceptor
    • Chapter 4: Interceptor execution order
    • Chapter 5: Creating an Interceptor for basic authentication checks

Chapter 1: Introduction to Spring Interceptors

  • Purpose: Intercept client requests before the controller processes them
  • Use Cases:
    • Logging HTTP requests and responses for debugging or tracking performance
    • Enforcing security policies (e.g., checking user authentication)
    • Caching HTTP requests and responses
    • Transforming HTTP requests and responses (e.g., converting JSON to XML)
  • Workflow in Spring Boot:
    • Request received by web server and forwarded to Spring Boot application
    • Dispatcher servlet receives request and consults URL Handler mapping
    • Handler mapping determines the appropriate controller

Chapter 2: Difference Between Spring Interceptors and Servlet Filters

  • Servlet Filters:
    • Part of Servlet API, operate at a lower level
    • Intercept request-response lifecycle, not specific to Spring Framework
  • Spring Interceptors:
    • Part of Spring Web MVC, require Spring application context
  • Deciding which to use:
    • Use Interceptors for tasks requiring Spring MVC
    • Use Filters for more generic tasks not requiring Spring

Chapter 3: Implementing and Registering an Interceptor

  • Creating Interceptor Class: Implements HandlerInterceptor interface
    • Methods:
      • preHandle: Executed before the controller, returns a boolean
      • postHandle: Executed after the controller but before the response is sent
      • afterCompletion: Executed after the request completion (for cleanup tasks)
  • Registering Interceptor:
    • Create a class implementing WebMvcConfigurer
    • Override addInterceptors method
    • Add Interceptor to InterceptorRegistry

Chapter 4: Interceptor Execution Order

  • Execution Sequence:
    • Multiple interceptors can be added
    • Specifying order using @Order annotation
    • First on pre-handle, reverse on post-handle and after completion
    • Example provided on how order affects execution

Chapter 5: Creating an Interceptor for Basic Authentication Checks

  • Objective: Ensure authenticated requests for certain endpoints
  • Steps:
    • Extract and decode the Authorization header
    • Validate username and password against predefined values
    • Implement logic in preHandle method for authentication checks

Example: Adding and Testing Interceptor

  • Creating LogHandlerInterceptor:
    • Implements HandlerInterceptor
    • Logs request flow in preHandle, postHandle, and afterCompletion methods
  • Registering LogHandlerInterceptor
  • Creating BasicAuthHandlerInterceptor:
    • Extends LogHandlerInterceptor
    • Performs basic authentication by validating against hardcoded credentials
  • Testing Interceptors:
    • Use Postman to test endpoints with and without correct credentials
    • Observe logs for sequence of interceptor execution

Conclusion

  • Summary of adding and using interceptors for various tasks in a Spring Boot application
  • Encouragement to ask questions and provide feedback
  • Subscribe for more content