HandlerInterceptorAdapter

spring 拦截器 

1.  

@Component
public class myInterceptor extends HandlerInterceptorAdapter

2.    重写此方法

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
myInterceptor annotation = method.getAnnotation(myInterceptor.class);
return null;
}
return super.preHandle(request, response, handler);

3. 注册此拦截器

@Configuration
public class MvcConfig implements WebMvcConfigurer

@Override
public void addInterceptors(InterceptorRegistry registry) {
log.info("注册拦截器");
registry.addInterceptor(myInterceptor).addPathPatterns(myInterceptor.split(","));
}

人总得做点什么 ,不是么
原文地址:https://www.cnblogs.com/liweibing/p/14846390.html