以下是我在学习 SpringBoot 拦截器时的一些记录,供本人与小伙伴们一起参考学习,有不对的地方请指正。
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {}
public class MyInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if(!(handler instanceof HandlerMethod)) {
return true;
}
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
if(annotation != null) {
}
return true;
}
}
@Configuration
public class WebConfiguration extends WebMvcConfigurationSupport {
@Bean
public MyInterceptor myInterceptor() {
return new MyInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(myInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}