博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
基于spring boot实现的通用注解式过滤器
阅读量:6005 次
发布时间:2019-06-20

本文共 3269 字,大约阅读时间需要 10 分钟。

hot3.png

实现基类

@Slf4jpublic abstract class BaseAnnotationInterceptor
{ Class
annotationClass; @SuppressWarnings("unchecked") @PostConstruct protected void PostConstruct() { annotationClass = (Class
) ReflectionUtils.getGenricType(this.getClass(), 0); if (log.isInfoEnabled()) { log.info("注解{},过滤器{}", annotationClass, this.getClass().getName()); } } public Class
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" })@Slf4jpublic class WebRequestInterceptor extends HandlerInterceptorAdapter {	@Autowired(required = false)	List
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
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
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
entry : getAnnotations(handlerMethod).entrySet()) { entry.getValue().afterCompletion(request, response, handlerMethod, ex, entry.getKey()); } } private Map
getAnnotations(HandlerMethod handlerMethod) { Map
map = new HashMap<>(); Map
, 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

你可能感兴趣的文章
MySQL数据库ab主从复制出错及解决过程
查看>>
一大型工厂网络规划方案
查看>>
常见的 css 问题
查看>>
基于LVM的数据库备份和恢复
查看>>
vtx
查看>>
免费素材网站
查看>>
Oracle---触发器
查看>>
微软超融合私有云测试32-SCCM2016部署之SCCM管理服务器安装
查看>>
reshape2包的进化版—tidyr包
查看>>
SaltStack (1)运维自动化部署
查看>>
chenfeiyao8七个问题测出你的全部
查看>>
网站程序文件打包及数据库备份上传到远程FTP脚本
查看>>
mysql的备份恢复
查看>>
mysql 传统版
查看>>
springboot项目 在服务器的启动方式
查看>>
Android Service的生命周期
查看>>
调优LAMP 应用程序的 5 种简单方法
查看>>
dos复制到剪切板
查看>>
SCCM2012R2部署之十:基于客户端请求的客户端安装
查看>>
编程中需要注意的几个原则吧
查看>>