app 請求帶上自身的「設備號id」 和手機號碼 //首先自定義註解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface SingleDeviceLogin { public String value() default "單點登陸"; } //在AOP中作切面,攔截左右標註註解的 方法 @Pointcut("@annotation(com.lifang.userapp.myannotation.SingleDeviceLogin)") public void annotationSignleDevice(){} //環繞通知:checkIsSigninLogin 主要作的是從redis中根據"手機號碼"獲取當前app的設備號; //若是沒有則從數據庫中獲取設備號; 若是和當前傳入的設備號不一樣則,isEntryMethod 爲false,不然爲true @Around("annotationSignleDevice()") public Object AroundMethod(ProceedingJoinPoint joinpoint) throws Throwable { Object obj = null; boolean isEntryMethod = this.checkIsSigninLogin(joinpoint); if (isEntryMethod) { obj = joinpoint.proceed(); //執行業務代碼 return obj; } else { Signature signaturn = joinpoint.getSignature(); Class returnType = ((MethodSignature)signaturn).getReturnType(); Object backNewInstance = returnType.newInstance(); Method methodStatus = returnType.getMethod("setStatus", int.class); Method methodMessage = returnType.getMethod("setMessage", String.class); methodStatus.invoke(backNewInstance, 2); methodMessage.invoke(backNewInstance, "該設備在別的手機上登陸,請從新登陸"); return backNewInstance; } } //登陸執行操做 把手機號和設備號 的對應關係放到 redis中 同時存入 數據庫中; //對應的攔截方法 @SingleDeviceLogin(value = "取消收藏信息") public Response uncollect(HttpServletRequest request, HttpServletResponse response, PersonConterRequest personConterRequest){}