自定义拦截器
package com.company.interceptor;import lombok.extern.slf4j.Slf4j;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@Slf4jpublic class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { log.debug("preHandle method is running"); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { log.debug("postHandle method is running"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { log.debug("afterCompletion method is running"); }}
写一个配置类
package com.company.config;import com.company.interceptor.MyInterceptor;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class MvcConfig implements WebMvcConfigurer{ @Override public void addInterceptors(InterceptorRegistry registry) { //指定拦截器类 //指定拦截路径 registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**"); }}
来源:https://www.cnblogs.com/zou-rong/p/12565244.html