定义注解类:
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface SignCheck { RequiredType required() default RequiredType.FORCE; }
RequiredType枚举类:
public enum RequiredType { /** * 强登录 */ FORCE, /** * 登录不等录都行 */ WEAK }
定义拦截器:
public class ApiSignInterceptor extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { SignCheck signCheck = InterceptorUtils.getAnnotation(handler, SignCheck.class); if (signCheck == null) { return true; } else { return signCheck(request, response); } } private boolean signCheck(HttpServletRequest request, HttpServletResponse response) { // 判断逻辑 return false; } }