在開發中,遇到限制登入次數或者限制使用者頻繁申請發送郵件或手機驗證碼的業務要求
關于手機驗證碼,語音驗證碼的實作,我是用雲通訊:http://www.yuntongxun.com/實作的
這裡總結一下如何防止使用者的重複操作。
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";
}
這是一種就防止重複操作的方法