自定义跳过拦截器注解

自定义跳过拦截器注解

Posted by catmai on January 22, 2020

自定义注解跳过拦截器

开发过程中有时候有些接口需要免登陆访问,跳过拦截器拦截。此时我们可以选择自定义注解跳过。这样在接口上加上注解就可以实现开放免登陆接口的功能了。

首先自定义一个注解,名称随便取,意思明确就行。

@Target(ElementType.METHOD)  
@Retention(RetentionPolicy.RUNTIME)  
@Inherited  
public @interface ExLoginIntercepter {  
  
  
}  

然后在拦截器的preHandle方法中添加如下代码:

HandlerMethod handlerMethod = (HandlerMethod) handler;  
//取到方法上的注解  
ExLoginIntercepter methodAnnotation = handlerMethod.getMethodAnnotation(ExLoginIntercepter.class);  
if (methodAnnotation != null){  
   return true;  
}  

在需要被过滤的方法上添加刚刚声明的注解。@ExLoginIntercepter,就可以绕开登录了。