SpringBoot在一定时间内限制接口请求次数的实现示例

作者:qq_41084438 时间:2021-10-12 04:28:52 

需要用到的知识:注解、AOP、ExpiringMap(带有有效期的映射)

我们可以自定义注解,把注解添加到我们的接口上。定义一个切面,执行方法前去ExpiringMap查询该IP在规定时间内请求了多少次,如超过次数则直接返回请求失败。

需要用到的依赖

<!-- AOP依赖 -->
<dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-aop</artifactId>
           <version>2.1.5.RELEASE</version>
</dependency>
<!-- Map依赖 -->
<dependency>
           <groupId>net.jodah</groupId>
           <artifactId>expiringmap</artifactId>
           <version>0.5.8</version>
</dependency>

自定义注解@LimitRequest

@Documented
@Target(ElementType.METHOD) // 说明该注解只能放在方法上面
@Retention(RetentionPolicy.RUNTIME)
public @interface LimitRequest {
   long time() default 6000; // 限制时间 单位:毫秒
   int count() default 1; // 允许请求的次数
}

自定义AOP

@Aspect
@Component
public class LimitRequestAspect {

private static ConcurrentHashMap<String, ExpiringMap<String, Integer>> book = new ConcurrentHashMap<>();

// 定义切点
   // 让所有有@LimitRequest注解的方法都执行切面方法
   @Pointcut("@annotation(limitRequest)")
   public void excudeService(LimitRequest limitRequest) {
   }

@Around("excudeService(limitRequest)")
   public Object doAround(ProceedingJoinPoint pjp, LimitRequest limitRequest) throws Throwable {

// 获得request对象
       RequestAttributes ra = RequestContextHolder.getRequestAttributes();
       ServletRequestAttributes sra = (ServletRequestAttributes) ra;
       HttpServletRequest request = sra.getRequest();

// 获取Map对象, 如果没有则返回默认值
       // 第一个参数是key, 第二个参数是默认值
       ExpiringMap<String, Integer> uc = book.getOrDefault(request.getRequestURI(), ExpiringMap.builder().variableExpiration().build());
       Integer uCount = uc.getOrDefault(request.getRemoteAddr(), 0);

if (uCount >= limitRequest.count()) { // 超过次数,不执行目标方法
           return "接口请求超过次数";
       } else if (uCount == 0){ // 第一次请求时,设置有效时间
//            /** Expires entries based on when they were last accessed */
//            ACCESSED,
//            /** Expires entries based on when they were created */
//            CREATED;
           uc.put(request.getRemoteAddr(), uCount + 1, ExpirationPolicy.CREATED, limitRequest.time(), TimeUnit.MILLISECONDS);
       } else { // 未超过次数, 记录加一
           uc.put(request.getRemoteAddr(), uCount + 1);
       }
       book.put(request.getRequestURI(), uc);

// result的值就是被拦截方法的返回值
       Object result = pjp.proceed();

return result;
   }

}

第一个静态Map是多线程安全的Map(ConcurrentHashMap),它的key是接口对于的url,它的value是一个多线程安全且键值对是有有效期的Map(ExpiringMap)。

ExpiringMap的key是请求的ip地址,value是已经请求的次数。

ExpiringMap更多的使用方法可以参考:https://github.com/jhalterman/expiringmap

SpringBoot在一定时间内限制接口请求次数的实现示例

最后在方法上面加上@LimitRequest就行了

SpringBoot在一定时间内限制接口请求次数的实现示例

来源:https://blog.csdn.net/qq_41084438/article/details/109014344

标签:SpringBoot,限制,接口,请求次数
0
投稿

猜你喜欢

  • 详解C语言实现猜数字游戏

    2023-11-03 04:58:51
  • Java中的Map集合简单汇总解析

    2023-08-22 19:12:07
  • java Springboot实现多文件上传功能

    2023-11-09 04:31:32
  • SpringCloud搭建netflix-eureka微服务集群的过程详解

    2023-09-02 18:11:52
  • Maven入门之使用Nexus搭建Maven私服及上传下载jar包

    2022-05-06 20:47:43
  • Java基础之容器Vector详解

    2023-11-25 13:10:07
  • 5个主流的Java开源IDE工具详解

    2021-10-13 06:06:50
  • 举例讲解Java中synchronized关键字的用法

    2023-07-01 22:20:19
  • 一篇文章带你入门Java数据类型

    2022-06-10 09:25:44
  • springboot schedule 解决定时任务不执行的问题

    2021-05-30 05:00:38
  • JVM教程之内存管理和垃圾回收(三)

    2023-11-10 15:49:54
  • SpringBoot深入分析讲解监听器模式上

    2022-06-25 21:04:04
  • java中Class.forName的作用浅谈

    2023-11-11 12:30:26
  • Java基础之Thymeleaf的简单使用

    2023-08-24 19:00:22
  • gson对象序列化的示例

    2023-11-25 08:54:28
  • Spring @Retryable注解轻松搞定循环重试功能

    2022-12-17 06:07:51
  • 如何利用IDEA搭建SpringBoot项目整合mybatis实现简单的登录功能

    2022-01-15 06:14:59
  • Java Spring MVC 上传下载文件配置及controller方法详解

    2023-11-22 04:13:59
  • Vs2022环境下安装低版本.net framework的实现步骤

    2023-07-04 02:58:12
  • JavaWeb中使用JavaMail实现发送邮件功能实例详解

    2023-01-07 13:54:37
  • asp之家 软件编程 m.aspxhome.com