In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you the "sample analysis of multiple interceptor configuration and use scenarios in SpringBoot2", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let me lead you to study and learn "sample analysis of multiple interceptor configuration and use scenarios in SpringBoot2".
Brief introduction of interceptor 1. Definition of interceptor
Interceptor, before the requested interface is accessed, intercept and then add some operation before or after. Interception is an implementation strategy of AOP. Interceptors are mainly used to reject requests in accordance with specified rules.
2. Apply token token verification request data verification user rights verification and release specified interface 2, SpringBoot2.0 interceptor usage 1, write two interceptors
Custom classes implement the HandlerInterceptor interface
1) OneInterceptor interceptor
Import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * interceptor 1 * / public class OneInterceptor implements HandlerInterceptor {private static final Logger LOGGER = LoggerFactory.getLogger (OneInterceptor.class.getName ()); @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {String url = String.valueOf (request.getRequestURL ()) LOGGER.info ("1, url==" + url); / / Let go of intercepting return true;} @ Override public void postHandle (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {LOGGER.info ("1, postHandle");} @ Override public void afterCompletion (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {LOGGER.info ("1, afterCompletion");}}
2) TwoInterceptor interceptor
Import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * interceptor 2 * / public class TwoInterceptor implements HandlerInterceptor {private static final Logger LOGGER = LoggerFactory.getLogger (TwoInterceptor.class.getName ()); @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {String url = String.valueOf (request.getRequestURL ()) LOGGER.info ("2, url==" + url); / / unblock return true;} @ Override public void postHandle (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {LOGGER.info ("2, postHandle");} @ Override public void afterCompletion (HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {LOGGER.info ("2, afterCompletion");}} 2. Injection interceptor import com.boot.intercept.intercept.OneInterceptor;import com.boot.intercept.intercept.TwoInterceptor into Web configuration file Import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/** * Web configuration file * / @ Configurationpublic class WebMvcConfig implements WebMvcConfigurer {public void addInterceptors (InterceptorRegistry registry) {/ / intercept all paths / / register custom two interceptors registry.addInterceptor (new OneInterceptor ()) .addPathPatterns ("/ * *"); registry.addInterceptor (new TwoInterceptor ()) .addPathPatterns ("/ * *") 3. Write the test interface import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class InterceptController {@ RequestMapping ("/ reqUrl") public String reqUrl () {return "success";} 4, access the test interface
The log output is as follows
Intercept.OneInterceptor: 1 、 url== http://127.0.0.1:8005/reqUrlintercept.TwoInterceptor: 2 、 url== http://127.0.0.1:8005/reqUrlintercept.TwoInterceptor: 2 、 postHandleintercept.OneInterceptor: 1 、 postHandleintercept.TwoInterceptor: 2 、 afterCompletionintercept.OneInterceptor: 1 、 afterCompletionla
The interception order of interceptors is performed in the order in which interceptors are injected into the Web configuration file.
The above is all the content of the article "sample Analysis of multiple interceptor configuration and usage scenarios in SpringBoot2". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.