第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");
}
}