Spring Boot implements custom interceptor steps:
1. Create our own interceptor class and implement the HandlerInterceptor interface.
public class MyInterceptor implements HandlerInterceptor{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
2. Create a Java class that inherits WebMvcConfigurerAdapter and override the addInterceptors method.
3. Instantiate our custom interceptor, and then manually add the object to the interceptor chain (in the addInterceptors method).
@Configuration public class WebInterceptorConfigurer extends WebMvcConfigurerAdapter { // addPathPatterns is used to add interception rules // excludePathPatterns user exclude interception @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**"); super.addInterceptors(registry); } }
Note: The interceptor chain will only go through the request of DispatcherServlet, and our custom Servlet request will not be intercepted.
http://blog.csdn.net/catoop/article/details/50501696