The difference between onceperrequestfilter and webmvcconfigurer

Insert image description here

Overview

When using the Spring framework for web development, we often encounter situations where we need to perform some unified processing on each request. For example, we might need to authenticate each request before it reaches the Controller, or log information about the request after each request ends. At this time, we can use two different ways to implement these functions: onceperrequestfilter and webmvcconfigurer.

onceperrequestfilter is a filter that intercepts each request and performs some operations before or after the request reaches the Controller. This filter will only be executed once, so it is typically used for operations that need to be executed only once during the entire request lifecycle, such as authentication, request logging, etc. In the Spring framework, we can implement our own filter by inheriting the OncePerRequestFilter class.

webmvcconfigurer is a configuration class that can globally modify the configuration of Spring MVC. In this configuration class, we can define some interceptors to process each request uniformly. These interceptors can perform some operations before or after the request reaches the Controller, such as authentication, request logging, etc. Unlike onceperrequestfilter, webmvcconfigurer can define multiple interceptors, and these interceptors can be executed in sequence. In the Spring framework, we can implement our own configuration class by implementing the WebMvcConfigurer interface.

So, what is the difference between onceperrequestfilter and webmvcconfigurer? The main points are as follows:

  1. onceperrequestfilter is a filterÿ

おすすめ

転載: blog.csdn.net/uniquewonderq/article/details/130904556