关于使用注解设置token免拦截

先说一下这样子做的原理:将某一个注解配置在方法头部,在spring实例化的时候会将注解以切面的形式注入给方法,在拦截的地方判断当前方法有没有注入指定的注解类。

1.先声明一个注解类(类中不需要做任何逻辑操作)

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TokenNotValidation{
}

2.在你的token拦截类中做一个判断设

public class intercept extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        TokenNotValidation annotation = ((HandlerMethod)   handler).getMethodAnnotation(TokenNotValidation.class);
        // 如果有@TokenNotValidation ,则不验证token
        if (annotation != null) {
            return true;
        }
    }

}

3.最后在你不需要拦截的方法头部加一个@TokenNotValidation就OK了

猜你喜欢

转载自blog.csdn.net/u012929855/article/details/80614499
今日推荐