基于spring boot实现的通用注解式过滤器

本文详细介绍了一种基于Spring框架的拦截器实现方案,通过自定义BaseAnnotationInterceptor类,可以针对特定注解进行请求前、后及完成后的处理。WebRequestInterceptor类作为具体实现,能够注册多个拦截器并应用于所有路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实现基类

@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对应

过滤器实现

@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("/**");配置过滤器

转载于:https://my.oschina.net/u/1428688/blog/2252569

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值