1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public class CustomizerHandlerInterceptor implements HandlerInterceptor { private static final Logger logger = LoggerFactory.getLogger(CustomizerHandlerInterceptor.class);
@Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String contextPath = request.getRequestURI();
if (handler instanceof HandlerMethod) { AopAnnotation annotation = findAnnotation((HandlerMethod) handler, AopAnnotation.class); if (annotation != null) { logger.info("preHandle, url={}", contextPath); } } return true; }
@Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { }
@Override public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { }
private static <A extends Annotation> A findAnnotation(HandlerMethod handler, Class<A> annotationType) { if (handler == null) { return null; } else { A annotation = handler.getBeanType().getAnnotation(annotationType); if (annotation == null) { annotation = handler.getMethodAnnotation(annotationType); }
if (annotation == null) { Class<?> clazz = handler.getBean().getClass(); if (clazz != null) { annotation = clazz.getAnnotation(annotationType); } }
return annotation; } } }
|