The interceptor

Spring’s HandlerInterceptor is a reflection mechanism based on Java, which is an application of AOP.

For example,

@Component
public class DemoInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // Perform some operations before the method (controller)
    }
    @Override
    public void postHandle( HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // Perform some operations after the method (controller)}}Copy the code

The Interceptor executes after the Filter.

Spring Boot has many interceptors built in.

scenario

While it is recommended to use interceptors to process requests before and after, filters are more versatile.