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

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

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


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

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); Token token = ((HandlerMethod) handler).getMethodAnnotation(Token.class); // 如果有@TokenNotValidation ,则不验证token if (annotation != null) { return true; } // 或者需要做权限认真,就认证 if (annotation != null) { //开始权限的逻辑判断............. return true; } } }

 

3.最后在你不需要拦截的方法头部加一个@TokenNotValidation,或者在需要认证的地方加@Token就可以了!

 

相关文章:

  • 2022-12-23
  • 2021-12-25
  • 2022-12-23
  • 2021-06-07
  • 2021-11-17
  • 2022-12-23
  • 2022-01-31
  • 2022-03-05
猜你喜欢
  • 2021-08-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
  • 2022-12-23
  • 2021-11-23
相关资源
相似解决方案