AuthAspect.java 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package com.ruoyi.system.utils;
  2. import com.ruoyi.common.core.domain.AjaxResult;
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.aspectj.lang.annotation.Around;
  5. import org.aspectj.lang.annotation.Aspect;
  6. import org.aspectj.lang.annotation.Pointcut;
  7. import org.aspectj.lang.reflect.MethodSignature;
  8. import org.springframework.stereotype.Component;
  9. import jakarta.annotation.Resource;
  10. import jakarta.servlet.http.HttpServletRequest;
  11. /**
  12. * Created with IntelliJ IDEA.
  13. *
  14. * @Auther: ZouTiancong
  15. * @Date: 2022/05/23/22:39
  16. * @Description:
  17. */
  18. @Component
  19. @Aspect
  20. public class AuthAspect {
  21. private static HttpServletRequest request;
  22. @Resource
  23. public void setHttpServletRequest(HttpServletRequest request) {
  24. AuthAspect.request = request;
  25. }
  26. @Pointcut("@annotation(com.ruoyi.system.utils.Auth)")
  27. private void authPointcut() {
  28. }
  29. @Around("authPointcut()")
  30. public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
  31. // 获取方法签名信息从而获取方法名和参数类型
  32. MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
  33. // 获取目标方法对象上注解中的属性值
  34. Auth auth = methodSignature.getMethod().getAnnotation(Auth.class);
  35. // 校验签名
  36. if (auth.token()) {
  37. boolean token = JwtUtil.verify(request.getHeader("token"));
  38. // 如果token校验失败返回
  39. if (!token) {
  40. return AjaxResult.error(401,"token已过期,请重新登录!");
  41. }
  42. }
  43. return joinPoint.proceed();
  44. }
  45. }