public @interface CustomAnnotation {
}
public class CustomInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 获取注解信息
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
CustomAnnotation annotation = handlerMethod.getMethod().getAnnotation(CustomAnnotation.class);
if (annotation != null) {
// 拦截处理逻辑
// ...
}
}
return true;
}
}
public class CustomInterceptorConfig implements WebMvcConfigurer {
@Autowired
private CustomInterceptor customInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(customInterceptor)
.addPathPatterns("/**");
}
}