当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot中自定义注解实现控制器访问次数限制实例

SpringBoot中自定义注解实现控制器访问次数限制实例

2019年07月22日  | 移动技术网IT编程  | 我要评论

饥饿游戏txt,无字拼图6,女教师性史

今天给大家介绍一下springboot中如何自定义注解实现控制器访问次数限制。

在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; 
} 

步骤二:定义一个异常类,用来处理url攻击时产生的异常问题,下面看代码事例:

package example.controller.exception; 
public class requestlimitexception extends exception { 
  private static final long serialversionuid = 1364225358754654702l; 
 
  public requestlimitexception() { 
    super("http请求超出设定的限制"); 
  } 
 
  public requestlimitexception(string message) { 
    super(message); 
  } 
 
} 

步骤三:定义一个注解的具体实现类,下面看代码事例:

package example.controller.limit; 
import example.controller.exception.requestlimitexception; 
import org.aspectj.lang.joinpoint; 
import org.aspectj.lang.annotation.aspect; 
import org.aspectj.lang.annotation.before; 
import org.slf4j.logger; 
import org.slf4j.loggerfactory; 
import org.springframework.stereotype.component; 
import javax.servlet.http.httpservletrequest; 
import java.util.hashmap; 
import java.util.map; 
import java.util.timer; 
import java.util.timertask; 
import java.util.concurrent.timeunit; 
 
@aspect 
@component 
public class requestlimitcontract { 
  private static final logger logger = loggerfactory.getlogger("requestlimitlogger"); 
  private map<string, integer> redistemplate=new hashmap<string,integer>(); 
  @before("within(@org.springframework.stereotype.controller *) && @annotation(limit)") 
  public void requestlimit(final joinpoint joinpoint, requestlimit limit) throws requestlimitexception { 
    try { 
      object[] args = joinpoint.getargs(); 
      httpservletrequest request = null; 
      for (int i = 0; i < args.length; i++) { 
        if (args[i] instanceof httpservletrequest) { 
          request = (httpservletrequest) args[i]; 
          break; 
        } 
      } 
      if (request == null) { 
        throw new requestlimitexception("方法中缺失httpservletrequest参数"); 
      } 
      string ip = request.getlocaladdr(); 
      string url = request.getrequesturl().tostring(); 
      string key = "req_limit_".concat(url).concat(ip); 
      if(redistemplate.get(key)==null || redistemplate.get(key)==0){ 
        redistemplate.put(key,1); 
      }else{ 
        redistemplate.put(key,redistemplate.get(key)+1); 
      } 
      int count = redistemplate.get(key); 
      if (count > 0) { 
        timer timer= new timer(); 
        timertask task = new timertask(){  //创建一个新的计时器任务。 
          @override 
          public void run() { 
            redistemplate.remove(key); 
          } 
        }; 
        timer.schedule(task, limit.time()); 
        //安排在指定延迟后执行指定的任务。task : 所要安排的任务。10000 : 执行任务前的延迟时间,单位是毫秒。 
      } 
      if (count > limit.count()) { 
        //logger.info("用户ip[" + ip + "]访问地址[" + url + "]超过了限定的次数[" + limit.count() + "]"); 
        throw new requestlimitexception(); 
      } 
    } catch (requestlimitexception e) { 
      throw e; 
    } catch (exception e) { 
      logger.error("发生异常: ", e); 
    } 
  } 
} 

步骤四:实现一个控制类,并添加使用注解功能。下面看代码事例:

package example.controller; 
import example.controller.limit.requestlimit; 
import org.springframework.stereotype.controller; 
import org.springframework.ui.modelmap; 
import org.springframework.web.bind.annotation.requestmapping; 
import org.springframework.web.bind.annotation.responsebody; 
import javax.servlet.http.httpservletrequest; 
@controller 
public class urlcontroller { 
  @requestlimit(count=10,time=5000) 
  @requestmapping("/urltest") 
  @responsebody 
  public string test(httpservletrequest request, modelmap modelmap) { 
    return "aaa"; 
  } 
} 

 其中count指的是规定时间内的访问次数,time指的就是规定时间,单位为毫秒。

这样就实现了在控制器这个层次上面的url拦截了。不过这里有个问题,就是如果想在每一个url页面上面都进行这样的拦截,这种方法明显是不够的。因为我们不可能在每个控制器上面都加上url拦截的注解,所以这种方法只适合在某些特定的url拦截上面使用它们。

那如何实现过滤器级别上面的url访问拦截呢?这里先给大家卖一个关子,我将会在下一节中给大家介绍如何利用过滤器实现url访问拦截,并且利用jpa实现ip黑名单的功能,加入ip黑名单后就不可以进行任何url的访问了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网