Spring Boot(05)自定义interceptor

tech2024-04-04  60

第05篇:自定义interceptor

流程

定义拦截器配置

1. interceptor class

public class PrintInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("interceptor preHandle, url: " + request.getRequestURI()); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("interceptor postHandle, url: " + request.getRequestURI()); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("interceptor afterCompletion, url: " + request.getRequestURI()); } }

2. 配置

@Configuration public class InterceptorConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new PrintInterceptor()).addPathPatterns("/index"); } }
最新回复(0)