2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > java 密码登录输入错误4次 锁定用户账号30分钟

java 密码登录输入错误4次 锁定用户账号30分钟

时间:2022-03-28 09:28:09

相关推荐

java 密码登录输入错误4次 锁定用户账号30分钟

Map err = new HashMap();if (!checkLock(session, UserId)) {err.put("err", false);err.put("msg", "该账号已被锁定,请30分钟后重试!");} else {if (用户名及密码匹配) {err.put("err", true);err.put("msg", "登录成功!");// 清空登录失败记录cleanFailNum(session, UserId);} else {// 新增登录失败记录String msg = addFailNum(session, UserId);err.put("err", false);err.put("msg", msg);}}/*** 校验用户登录失败次数* @param session* @param userId* @return*/private boolean checkLock(HttpSession session,String userId) {Object o = session.getServletContext().getAttribute(userId);if(o == null) {return true;}HashMap<String,Object> map = (HashMap<String, Object>) o;int num = Integer.parseInt(Pub.IsNull(map.get("num")));Date date = (Date) map.get("lastDate");long timeDifference = ((new Date().getTime() - date.getTime()) / 60 / 1000);if(num >= 4 && timeDifference < 30) {return false;}return true;}/*** 新增用户登录失败次数* @param session* @param userId*/private String addFailNum(HttpSession session, String userId) {Object o = session.getServletContext().getAttribute(userId);HashMap<String,Object> map = null;int num= 0;if(o == null) {map = new HashMap<String,Object>();}else {map = (HashMap<String, Object>) o;num = Integer.parseInt(Pub.IsNull(map.get("num")));Date date = (Date) map.get("lastDate");long timeDifference = ((new Date().getTime() - date.getTime()) / 60 / 1000);if(timeDifference >= 30) {num = 0;}}map.put("num", num+1);map.put("lastDate", new Date());session.getServletContext().setAttribute(userId, map);String returnStr = "";if (0 != 4-(num+1)) {returnStr = "密码错误,您还有"+(4-(num+1))+"次机会!";} else {returnStr = "该账号已被锁定,请30分钟后重试!";}return returnStr;}/*** 清理用户登录失败的记录* @param session* @param userId*/private void cleanFailNum(HttpSession session, String userId) {session.getServletContext().removeAttribute(userId);}

参考:JAVA密码输入错误3次,锁定账号30分钟简单实现(不操作数据库)_eclipse用户登录三次失败锁死_Man1217的博客-CSDN博客

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。