ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 配置拦截器 ~~~ package com.like.interecptor; 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; public class MyInterceptor implements HandlerInterceptor { //使用slf4j日志 private static final Logger log = LoggerFactory.getLogger(MyInterceptor.class); //传入类 @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { log.debug("preHandle running"); //这里使用的级别不能低于application.yml的日志级别,否则看不到输出 return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { log.debug("postHandle running"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { log.debug("afterCompletion running"); } } ~~~ ## 注册拦截器 ~~~ package com.like.config; import com.like.interecptor.MyInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class MvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { //注册拦截器 registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**"); } } ~~~ ## 结果 ``` 2019-04-27 17:38:25.677 DEBUG 25893 --- [nio-9000-exec-2] com.like.interecptor.MyInterceptor : preHandle running 2019-04-27 17:38:25.680 DEBUG 25893 --- [nio-9000-exec-2] com.like.interecptor.MyInterceptor : postHandle running 2019-04-27 17:38:25.680 DEBUG 25893 --- [nio-9000-exec-2] com.like.interecptor.MyInterceptor : afterCompletion running ```