LoginCheckAspect.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package com.studenthotel.aop;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. import org.aspectj.lang.annotation.After;
  4. import org.aspectj.lang.annotation.Around;
  5. import org.aspectj.lang.annotation.Aspect;
  6. import org.aspectj.lang.annotation.Before;
  7. import org.springframework.core.annotation.Order;
  8. import org.springframework.stereotype.Component;
  9. /**
  10. * @Author: binguo
  11. * @Date: 2023/4/10 星期一 14:10
  12. * @Description: com.video.aop
  13. * @Version: 1.0
  14. */
  15. @Aspect//把当前类标识作为一个切面供容器读取
  16. @Component//将此类标记为Spring容器中的一个Bean
  17. @Order(0)//order的值越小,优先级越高 order如果不标注数字,默认最低优先级,因为其默认值是int最大值
  18. public class LoginCheckAspect {
  19. @Around("@annotation(com.studenthotel.annotation.UserLoginCheck)")
  20. public Object userLoginCheck(ProceedingJoinPoint process) throws Throwable {
  21. System.out.println("用户登录检测机制");
  22. if(1 == 1){
  23. //通过抛异常方式拦截
  24. // throw new Exception("非法登录");
  25. }
  26. Object proceed;
  27. proceed = process.proceed();//执行目标方法
  28. return proceed;//将执行目标方法后的返回值返回出去
  29. }
  30. @After("@annotation(com.studenthotel.annotation.UserLoginCheck)")
  31. public void test(){
  32. System.out.println("测试After");
  33. }
  34. @Before("@annotation(com.studenthotel.annotation.UserLoginCheck)")
  35. public void test1(){
  36. System.out.println("测试Before");
  37. }
  38. }