Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of twice execution of springboot interceptor

Shulou Source: shulou.com Published: 2022-06-03 13:34:30 10月03日 Update

Editor to share with you two examples of springboot interceptor implementation of the analysis, I hope you will learn something after reading this article, let's discuss it!

The springboot interceptor executes twice

The reason is:

Org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error

It is also a controller path with / error

@ Configurationpublic class MVCConfig extends WebMvcConfigurationSupport {/ / Custom interceptor @ Bean public SecurityInterceptor getSecurityInterceptor () {return new SecurityInterceptor ();} @ Override public void addInterceptors (InterceptorRegistry registry) {/ / add interceptor InterceptorRegistration registration = registry.addInterceptor (getSecurityInterceptor ()); / / excluded path registration.excludePathPatterns ("/ login"); registration.excludePathPatterns ("/ logout") / / release this controller to registration.excludePathPatterns ("/ error"); / / intercept all registration.addPathPatterns ("/ *");}} Springboot interceptor principle

Based on the current request, find * * HandlerExecutionChain**** [all interceptors that can process the requested handler and handler]

Let's first sequentially execute the preHandle method of all interceptors

If the current interceptor prehandler returns as true. Then execute the preHandle of the next interceptor

If the current interceptor returns false. Directly reverse the afterCompletion of all interceptors that have been executed

If any interceptor returns false. Directly jump out of the non-execution target method

All interceptors return True. Execute the target method

Executes the postHandle methods of all interceptors in reverse order.

Any exception in the previous step will directly trigger afterCompletion in reverse order.

After the page is successfully rendered, afterCompletion will also be triggered in reverse order.

After reading this article, I believe you have a certain understanding of "sample Analysis of springboot interceptor execution twice". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!

Tags: Interceptors reverse order methods examples analysis goals articles paths success reasons principles over more steps knowledge industry information information channels pages order Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Xiaomi MariaDB Redmi Microsoft