在开发中,遇到限制登录次数或者限制用户频繁申请发送邮件或手机验证码的业务要求
关于手机验证码,语音验证码的实现,我是用云通讯:/实现的
这里总结一下如何防止用户的重复操作。
java开发中,常用到vo,po的概念,vo为值对象,一般为传递值用,po与数据库字段对应,比如用户实体po常用字段提取为vo,查询数据库时只查vo里的字段用vo接收,这样是一种优化的手段,不然,直接查询数据库把所有用户的字段查询后放入session中是不明智的。
说正事,我们在一个用户vo实体User中,设置记录用户某一此操作的次数的字段private int sendMsgCount=0;提供set和get,再提供一个做计数的方法,如下:
private int sendMsgCount=0;public int getSendMsgCount() {return sendMsgCount;}public void setSendMsgCount(int sendMsgCount) {this.sendMsgCount = sendMsgCount;}public void increaseSendMsgCount() {this.sendMsgCount++;}
在业务逻辑中,进行相关次数判断:
User user = request.getSession().getAttribute("user");if (user == null) {user = new User();}user.increaseSendMsgCount();request.getSession().setAttribute("user");if (user.getSendMsgCount> 4) {logger.debug("消息发送频次超过五次");modelMap.addAttribute("msg", "您请求的业务消息发送频次超过五次");return "user/login";}
这是一种就防止重复操作的方法