基於spring boot實現的通用註解式過濾器

實現基類java

@Slf4j
public abstract class BaseAnnotationInterceptor<T extends Annotation> {

	Class<T> annotationClass;

	@SuppressWarnings("unchecked")
	@PostConstruct
	protected void PostConstruct() {
		annotationClass = (Class<T>) ReflectionUtils.getGenricType(this.getClass(), 0);
		if (log.isInfoEnabled()) {
			log.info("註解{},過濾器{}", annotationClass, this.getClass().getName());
		}
	}

	public Class<T> getAnnotationClass() {
		return annotationClass;
	}

	public abstract boolean preHandle(HttpServletRequest request, HttpServletResponse response,
			HandlerMethod handlerMethod, T annotation);

	public void postHandle(HttpServletRequest request, HttpServletResponse response, HandlerMethod handler,
			ModelAndView modelAndView, T annotation) {
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, HandlerMethod handler, Exception ex,
			T annotation) {
	}

}

這裏的方法名與spring中HandlerInterceptorAdapter對應web

過濾器實現spring

@Component
@SuppressWarnings({ "rawtypes", "unchecked" })
@Slf4j
public class WebRequestInterceptor extends HandlerInterceptorAdapter {

	@Autowired(required = false)
	List<BaseAnnotationInterceptor> interceptors;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		if (!(handler instanceof HandlerMethod) || interceptors == null) {
			return true;
		}
		HandlerMethod handlerMethod = (HandlerMethod) handler;
		for (Map.Entry<Annotation, BaseAnnotationInterceptor> entry : getAnnotations(handlerMethod).entrySet()) {
			if (!entry.getValue().preHandle(request, response, handlerMethod, entry.getKey())) {
				if (log.isDebugEnabled()) {
					log.debug("請求{}被攔截,class={}", request.getRequestURI(), entry.getValue().getClass().getName());
				}
				return false;
			}
		}
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if (!(handler instanceof HandlerMethod) || interceptors == null) {
			return;
		}
		HandlerMethod handlerMethod = (HandlerMethod) handler;
		for (Map.Entry<Annotation, BaseAnnotationInterceptor> entry : getAnnotations(handlerMethod).entrySet()) {
			entry.getValue().postHandle(request, response, handlerMethod, modelAndView, entry.getKey());
		}
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		if (!(handler instanceof HandlerMethod) || interceptors == null) {
			return;
		}
		HandlerMethod handlerMethod = (HandlerMethod) handler;
		for (Map.Entry<Annotation, BaseAnnotationInterceptor> entry : getAnnotations(handlerMethod).entrySet()) {
			entry.getValue().afterCompletion(request, response, handlerMethod, ex, entry.getKey());
		}
	}

	private Map<Annotation, BaseAnnotationInterceptor> getAnnotations(HandlerMethod handlerMethod) {
		Map<Annotation, BaseAnnotationInterceptor> map = new HashMap<>();
		Map<Class<? extends Annotation>, Annotation> annotationMap = AnnotationUtil.getAnnotations(handlerMethod);
		for (BaseAnnotationInterceptor<?> interceptor : interceptors) {
			Annotation annotation = annotationMap.get(interceptor.getAnnotationClass());
			if (annotation != null) {
				map.put(annotation, interceptor);
			}
		}
		return map;
	}
}

registry.addInterceptor(webRequestInterceptor).addPathPatterns("/**");配置過濾器ide

相關文章
相關標籤/搜索