SpringBoot 实现控制器 IP 访问次数限制

在 Web 中最经常发生的就是利用恶性 URL 访问刷爆服务器之类的***,今天我就给大家介绍一下如何利用自定义注解实现这类***的防御操作。

其实这类问题一般的解决思路就是:在控制器中加入自定义注解实现访问次数限制的功能。

具体的实现过程看下面的例子:

package example.controller.limit;  
import org.springframework.core.Ordered;  
import org.springframework.core.annotation.Order;  
import java.lang.annotation.*;  
  @Retention(RetentionPolicy.RUNTIME)  
  @Target(ElementType.METHOD)  
  @Documented  
  //最高优先级  
   @Order(Ordered.HIGHEST_PRECEDENCE)  
  public @interface RequestLimit {  
      /** 
       * 
       * 允许访问的次数,默认值MAX_VALUE 
       */  
      int count() default Integer.MAX_VALUE;  
    
      /** 
       * 
       * 时间段,单位为毫秒,默认值一分钟 
       */  
      long time() default 60000;  
  }