Spring Filter And Interceptor

Where we have filter and Interceptor. 




Interceptor

Create an Interceptor


Register Interceptor



Sample Controller



Output


Filter

Just Create a filter by extending the filter class and we can order them by using order annotation.





When we execute the application, we can see in the output that filters execute first an then interceptors.


Interceptors are part of the Spring MVC framework and sit between the DispatcherServlet and our Controllers. We can intercept requests before they reach our controllers, and before and after the view is rendered.


The key takeaway is that with Filters, we can manipulate requests before they reach our controllers and outside of Spring MVC. Otherwise, HandlerInterceptors are a great place for application-specific cross-cutting concerns. By providing access to the target Handler and ModelAndView objects, we have more fine-grained control.

Filters intercept requests before they reach the DispatcherServlet, making them ideal for coarse-grained tasks such as:

1) Authentication
2) Logging and auditing
3) Image and data compression
4) Any functionality we want to be decoupled from Spring MVC

HandlerIntercepors, on the other hand, intercepts requests between the DispatcherServlet and our Controllers. This is done within the Spring MVC framework, providing access to the Handler and ModelAndView objects. This reduces duplication and allows for more fine-grained functionality such as:

1) Handling cross-cutting concerns such as application logging
2) Detailed authorization checks
3) Manipulating the Spring context or model
   









Comments

Popular posts from this blog

Java 8 : Find the number starts with 1 from a list of integers

Junit Mockito and Power Mockito

Important Linux Commands