private static final int SEND_COUNT = 10;//10 public boolean expiredIPCount(HttpSession session, ServletRequest request) throws Exception { String ip = request.getRemoteAddr(); if (StringUtils.isEmpty(ip)) { return true; } if (session != null) { String oldIP = (String) session.getAttribute(“IP”); Integer ipSendCount = (Integer) session.getAttribute(“IPSendCount”); if (StringUtils.isNotBlank(oldIP) && oldIP.equals(ip)) { if (ipSendCount != null) { if (ipSendCount > SEND_COUNT) { return false; ///throw new Exception(“哎呀!发送验证码太频繁了,不要累着,请休息一下再发。”); } else { logger.info(“IP:” + ipSendCount); session.setAttribute(“IPSendCount”, ipSendCount + 1); return true; } } } } session.setAttribute(“IP”, ip); session.setAttribute(“IPSendCount”, 1); session.setMaxInactiveInterval(EXPIRED_IP_SEND_COUNT); return true; }
限制同一个IP请求次数
最新推荐文章于 2024-07-13 14:11:33 发布