通过注解实现调用指定的拦截器
来源:互联网 发布:淘宝运动兴奋剂 编辑:程序博客网 时间:2024/06/15 02:16
import java.lang.annotation.ElementType;import java.lang.annotation.Target;import java.text.MessageFormat;import javax.annotation.PostConstruct;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.lang3.ArrayUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.AsyncHandlerInterceptor;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;/** * 通过注解实现调用指定的拦截器 * * @author Saleson Lee. * @date 2016年3月19日 * @time 下午6:58:10 */public class AnnotationHandlerInterceptor extends HandlerInterceptorAdapter {private static final Logger LOG = LoggerFactory.getLogger(AnnotationHandlerInterceptor.class);/** * 需要过滤的注解类型 */@SuppressWarnings("rawtypes")private Class annotationClass;/** * 过滤器 */private AsyncHandlerInterceptor[] handlerInterceptors;/** * 检测类或方法的注解 */private AnnotationTarget annotationTarget;@SuppressWarnings("unchecked")@PostConstructpublic void init() throws Exception {if (this.annotationClass == null) {throw new Exception("属性annotationClass为空");} else if (handlerInterceptors == null) {throw new Exception("属性handlerInterceptors为空");}if (annotationTarget == null) {Target target = (Target) annotationClass.getAnnotation(Target.class);if (target == null) {throw new Exception(MessageFormat.format("注解{0}没有定义{1}注解",annotationClass, target));}ElementType[] eTypes = target.value();if (ArrayUtils.contains(eTypes, ElementType.METHOD)&& ArrayUtils.contains(eTypes, ElementType.TYPE)) {annotationTarget = AnnotationTarget.TYPE_AND_METHOD;} else if (ArrayUtils.contains(eTypes, ElementType.METHOD)) {annotationTarget = AnnotationTarget.METHOD;} else if (ArrayUtils.contains(eTypes, ElementType.TYPE)) {annotationTarget = AnnotationTarget.TYPE;} else {throw new Exception(MessageFormat.format("注解{0}中{1}注解没有定义{2}或{3}", annotationClass, target,ElementType.TYPE, AnnotationTarget.METHOD));}}printDebugInitLog();}@SuppressWarnings("unchecked")@Overridepublic boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {HandlerMethod method = (HandlerMethod) handler;boolean invokable = false;switch (annotationTarget) {case METHOD:invokable = method.getMethod().getAnnotation(annotationClass) != null;break;case TYPE:invokable = method.getBeanType().getAnnotation(annotationClass) != null;break;case TYPE_AND_METHOD:invokable = method.getMethod().getAnnotation(annotationClass) != null|| method.getBeanType().getAnnotation(annotationClass) != null;break;}if (invokable) {return invokeHandlerInterceptorsPreHandle(request, response,handler);}return true;}/** * 调用过滤器 * * @param request * @param response * @param handler * @return * @throws Exception */private boolean invokeHandlerInterceptorsPreHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception {for (AsyncHandlerInterceptor handlerInterceptor : handlerInterceptors) {if (!handlerInterceptor.preHandle(request, response,handler)) {return false;}}return true;}/** * @param handlerInterceptors * the handlerInterceptors to set */public void setHandlerInterceptors(AsyncHandlerInterceptor[] handlerInterceptors) {this.handlerInterceptors = handlerInterceptors;}/** * @param annotationClass * the annotationClass to set */public void setAnnotationClass(String annotationClass) {try {this.annotationClass = Class.forName(annotationClass);} catch (ClassNotFoundException e) {LOG.error(MessageFormat.format("找不到类 ==> {0}", annotationClass), e);}}/** * @param annotationTarget * the annotationTarget to set */public void setAnnotationTarget(AnnotationTarget annotationTarget) {this.annotationTarget = annotationTarget;}/** * 打印Debug日志 */private void printDebugInitLog() {switch (annotationTarget) {case TYPE_AND_METHOD:LOG.debug("拦截标有{}注解的Controller类和方法, 调用过滤器{}", annotationClass,handlerInterceptors);break;case TYPE:LOG.debug("拦截标有{}注解的Controller类, 调用过滤器{}", annotationClass,handlerInterceptors);break;case METHOD:LOG.debug("拦截标有{}注解的Controller类方法, 调用过滤器{}", annotationClass,handlerInterceptors);}}enum AnnotationTarget {TYPE_AND_METHOD, TYPE, METHOD}}
0 0
- 通过注解实现调用指定的拦截器
- 通过自定义注解或拦截器实现APP调用后接口全局权限验证
- spring拾遗(四)——自定义注解实现interceptor的指定拦截
- 自定义注解,实现拦截器
- springmvc自定义注解拦截器方式实现注解功能拦截器的配置
- 使用注解方式调用自定义拦截器
- 利用注解加拦截器实现struts2的权限设置
- 关于SSH注解开发 拦截器的配置以及实现
- Spring MVC拦截器通过注解方式实现防止表单重复提交
- springmvc-用java代码拦截指定注解的方法
- AOP的实现:Spring注解形式拦截
- java中,自定义注解拦截器来实现,在需要的拦截的方法上面加上一个注解@AccessRequired
- 拦截器+redis实现指定服务的次数现在及登录拦截
- 通过spring的拦截器实现Filter的功能
- Spring AOP实现之拦截器调用的实现
- Android下通过root实现对system_server中binder的ioctl调用拦截
- Android下通过root实现对system_server中binder的ioctl调用拦截
- Android下通过root实现对system_server中binder的ioctl调用拦截
- jquery 父页面判断子页面是否加载完成
- 【EmbeddedDev】BBB控制直流电机的转速与方向
- MouseDown事件
- 谁有这样的开发及运行平台
- iOS百度地图导航
- 通过注解实现调用指定的拦截器
- 2016.09.03【初中部 NOIP提高组 】模拟赛C
- 1111. Online Map (30)-PAT甲级真题(Dijkstra + DFS)
- ACM进阶学习计划
- 第K大数问题
- 用于时序数列预测的 Elman神经网络
- LINQ not in 写法
- Linux服务器升级tomcat 二
- N皇后问题(DFS)N皇后问题