RateLimitingAspect.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package com.backendsys.aspect;
  2. import com.backendsys.exception.CustException;
  3. import com.backendsys.modules.common.config.redis.utils.RedisUtil;
  4. import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
  5. import jakarta.servlet.http.HttpServletRequest;
  6. import lombok.RequiredArgsConstructor;
  7. import lombok.extern.slf4j.Slf4j;
  8. import org.aspectj.lang.JoinPoint;
  9. import org.aspectj.lang.annotation.Aspect;
  10. import org.aspectj.lang.annotation.Before;
  11. import org.aspectj.lang.reflect.MethodSignature;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.data.redis.core.RedisTemplate;
  14. import org.springframework.stereotype.Component;
  15. import java.lang.reflect.Method;
  16. import java.util.concurrent.TimeUnit;
  17. /**
  18. * 限流
  19. * @RateLimiting(key = "systemLoginWithPhone")
  20. * @RateLimiting(key = "systemLoginWithPhone", duration = 2, limit = 10)
  21. */
  22. @Slf4j
  23. @Aspect
  24. @Component
  25. @RequiredArgsConstructor
  26. public class RateLimitingAspect {
  27. @Autowired
  28. private RedisUtil redisUtil;
  29. @Autowired
  30. private HttpRequestUtil httpRequestUtil;
  31. /**
  32. * 带有注解的方法之前执行
  33. * - duration: 限流周期 (秒) (默认1秒内)
  34. * - limit: 限流次数 (默认1秒5次)
  35. */
  36. @SuppressWarnings("unchecked")
  37. @Before("@annotation(rateLimiting)")
  38. public void doBefore(RateLimiting rateLimiting) {
  39. // 限流标识: 方法名-IP-用户ID
  40. String key = "ratelimit-" + rateLimiting.key() + "-" + httpRequestUtil.getIpAddr() + '-' + httpRequestUtil.getUserId();
  41. String lockKey = "lock-" + key;
  42. // 已锁定
  43. Integer isLocked = redisUtil.getCacheObject(lockKey);
  44. if (isLocked != null) throw new CustException("访问过于频繁,请稍后再试!");
  45. // 未达到限流次数,自增
  46. long value = redisUtil.increment(key, 1);
  47. if (value > rateLimiting.limit()) {
  48. System.out.println("接口限流: " + key);
  49. redisUtil.setCacheObject(lockKey, 1, rateLimiting.lockDuration(), TimeUnit.SECONDS);
  50. throw new CustException("访问过于频繁,请稍后再试!");
  51. }
  52. // 第一次请求设置过期时间
  53. if (value == 1) redisUtil.expire(key, rateLimiting.duration(), TimeUnit.SECONDS);
  54. }
  55. }