[SpringMVC]自定义注解实现控制器访问次数限制

我们需要根据IP去限制用户单位时间的访问次数,防止刷手机验证码,屏蔽注册机等,使用注解就非常灵活了

1 定义注解

  1. @Retention(RetentionPolicy.RUNTIME)  
  2. @Target(ElementType.METHOD)  
  3. @Documented  
  4. //最高优先级  
  5. @Order(Ordered.HIGHEST_PRECEDENCE)  
  6. public @interface RequestLimit {  
  7.     /** 
  8.      *  
  9.      * 允许访问的次数,默认值MAX_VALUE 
  10.      */  
  11.     int count() default Integer.MAX_VALUE;  
  12.   
  13.     /** 
  14.      *  
  15.      * 时间段,单位为毫秒,默认值一分钟 
  16.      */  
  17.     long time() default 60000;  
  18. }  

 2 实现注解

  1. @Aspect  
  2. @Component  
  3. public class RequestLimitContract {  
  4.     private static final Logger logger = LoggerFactory.getLogger("RequestLimitLogger");  
  5.     @Autowired  
  6.     private RedisTemplate<String, String> redisTemplate;  
  7.   
  8.     @Before("within(@org.springframework.stereotype.Controller *) && @annotation(limit)")  
  9.     public void requestLimit(final JoinPoint joinPoint, RequestLimit limit) throws RequestLimitException {  
  10.   
  11.       try {  
  12.             Object[] args = joinPoint.getArgs();  
  13.             HttpServletRequest request = null;  
  14.             for (int i = 0; i < args.length; i++) {  
  15.                 if (args[i] instanceof HttpServletRequest) {  
  16.                     request = (HttpServletRequest) args[i];  
  17.                     break;  
  18.                 }  
  19.             }  
  20.             if (request == null) {  
  21.                 throw new RequestLimitException("方法中缺失HttpServletRequest参数");  
  22.             }  
  23.             String ip = HttpRequestUtil.getIpAddr(request);  
  24.             String url = request.getRequestURL().toString();  
  25.             String key = "req_limit_".concat(url).concat(ip);  
  26.             long count = redisTemplate.opsForValue().increment(key, 1);  
  27.             if (count == 1) {  
  28.                 redisTemplate.expire(key, limit.time(), TimeUnit.MILLISECONDS);  
  29.             }  
  30.             if (count > limit.count()) {  
  31.                 logger.info("用户IP[" + ip + "]访问地址[" + url + "]超过了限定的次数[" + limit.count() + "]");  
  32.                 throw new RequestLimitException();  
  33.             }  
  34.         } catch (RequestLimitException e) {  
  35.             throw e;  
  36.         } catch (Exception e) {  
  37.             logger.error("发生异常: ", e);  
  38.         }  
  39.     }  
  40. }  

3 自定义Exception

  1. public class RequestLimitException extends Exception {  
  2.     private static final long serialVersionUID = 1364225358754654702L;  
  3.   
  4.     public RequestLimitException() {  
  5.         super("HTTP请求超出设定的限制");  
  6.     }  
  7.   
  8.     public RequestLimitException(String message) {  
  9.         super(message);  
  10.     }  
  11.   
  12. }  

4 在Controller中使用

  1. @RequestLimit(count=100,time=60000)  
  2. @RequestMapping("/test")  
  3. public String test(HttpServletRequest request, ModelMap modelMap) {  
  4.     //TODO  
  5.  }  

 我使用了redis缓存访问次数,并且设置自增1,其实用静态map也可以。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值