zoukankan      html  css  js  c++  java
  • @interface [SpringMVC+redis]自定义aop注解实现控制器访问次数限制

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

    1 定义注解

    复制代码
    @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; }
    复制代码

     2 实现注解

    复制代码
    @Aspect
    @Component
    public class RequestLimitContract {
        private static final Logger logger = LoggerFactory.getLogger("RequestLimitLogger");
        @Autowired
        private RedisTemplate<String, String> redisTemplate;
    
        @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 = HttpRequestUtil.getIpAddr(request); String url = request.getRequestURL().toString(); String key = "req_limit_".concat(url).concat(ip); long count = redisTemplate.opsForValue().increment(key, 1); if (count == 1) { redisTemplate.expire(key, limit.time(), TimeUnit.MILLISECONDS); } 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); } } }
    复制代码

    3 自定义Exception

    复制代码
    public class RequestLimitException extends Exception {
        private static final long serialVersionUID = 1364225358754654702L;
    
        public RequestLimitException() {
            super("HTTP请求超出设定的限制");
        }
    
        public RequestLimitException(String message) {
            super(message);
        }
    
    }
    复制代码

    4 在Controller中使用

    @RequestLimit(count=100,time=60000)
    @RequestMapping("/test")
    public String test(HttpServletRequest request, ModelMap modelMap) {
        //TODO
     }

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

  • 相关阅读:
    Android SQLite 建立多表间的主外键关系 Binary
    Android——扩大ImageButton的点击区域 Binary
    Android——刷新媒体库 Binary
    Oracle数据导入导出imp/exp命令 10g以上expdp/impdp命令 转自:南水江的鸽子窝
    在同一应用中混合使用ASP.NET窗体和ASP.NET MVC 转自:geez的个人空间
    ASP.NET WebApplication 发布部署
    Oracle 导出索引Sequence!!
    获取ORACLE 表字段,表名,以及主键之类等等的信息
    FlexGrid 控件的使用 摘自:大道至简
    整合 DZNT到自己网站
  • 原文地址:https://www.cnblogs.com/zhoading/p/12199762.html
Copyright © 2011-2022 走看看