一共分两步
1-创建拦截器
2-注册拦截器
1-创建拦截器
@Slf4j
@Component
public class HttpInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("---------------------开始进入请求地址拦截----------------------------");
String url = request.getRequestURL().toString();
Map<String, String[]> parameterMap = request.getParameterMap();
log.info("request start url:{}, param{}",url,parameterMap.toString());
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.info("--------------处理请求完成后视图渲染之前的处理操作---------------");
String url = request.getRequestURL().toString();
Map<String, String[]> parameterMap = request.getParameterMap();
log.info("request finish url:{}, param{}",url,parameterMap.toString());
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.info("---------------视图渲染之后的操作-------------------------0");
String url = request.getRequestURL().toString();
Map<String, String[]> parameterMap = request.getParameterMap();
log.info("request exception url:{}, param{}",url,parameterMap.toString());
}
}
2-注册拦截器
@Configuration
@EnableWebMvc
@ComponentScan//组件扫描
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new HttpInterceptor()).addPathPatterns("/**").excludePathPatterns("/emp/toLogin","/emp/login","/js/**","/css/**","/images/**");
}
}