1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 实现登录密码输入错误次数过多 锁定用户账号 前台可以进行解锁的业务(未完善版本)

实现登录密码输入错误次数过多 锁定用户账号 前台可以进行解锁的业务(未完善版本)

时间:2021-02-28 00:18:02

相关推荐

实现登录密码输入错误次数过多 锁定用户账号 前台可以进行解锁的业务(未完善版本)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

前言一、前台实现解锁二、后台逻辑实现二、步骤1.数据库2.代码实现

前言

工作的第一天,就接到了一个登录锁定与解锁的需求,第一反应,这是什么,我要怎么做,怎么上来就让我写这么难的业务,哈哈。不过万物皆可百度,经过观看网上大佬们的实现方法,经过本人cop.,经过本人的借鉴,满足了需求,直接上代码吧。

一、前台实现解锁

前台就是加了个按钮,可以点击进行事件触发,把锁定的状态给它放开咯

前端不描述太多(本人前端太菜了,怕打脸( ̄ε(# ̄)☆╰╮( ̄▽ ̄///))

二、后台逻辑实现

首先就要梳理清业务的完整流程,既然是登录锁定,登录输入密码就会有两种情况,输入正确和不正确。先讨论正确时会有哪些情况,账号被锁定和未锁定,没有锁定直接进入系统,锁定的时候就要拦截不允许登录了;下边就是密码不正确,大前提肯定是不能登录,然后考虑锁定的状况,如果账号已经锁着了,那就还锁着(后续考虑改进一下,可以经过三十分钟或者一个小时,让其自动解锁),没有锁,进行错误次数增加,达到你规定的错误次数,进行锁定。

大致逻辑是这样

二、步骤

1.数据库

本人实现这个功能,在原来数据库表的情况下,增加了三个字段:last_login_error_time(最后一次登录错误时间),login_error_count(登录错误计数),is_locked(是否锁定):

2.代码实现

直接上代码,不玩虚的:

String nespwd = request.getParameter("nespwd");String code = request.getParameter("code");Person person = personDao.findByUserNameAndaesPassword(personName);if (code.equals(person.getCode())) {return "login";} else {person.setCode(code);personDao.save(person);}if (personName != null && nespwd != null) {Date thisErrorLoginTime = null; // 修改的本次登陆错误时间Integer islocked = 0; //定义锁定状态if (person == null) {return "login";} else if (!person.getAesPassword().equals(nespwd)) {if (person.getIsLocked() == null) {person.setIsLocked(0);} else {islocked = person.getIsLocked();}if (person.getLoginErrorCount() == null) {person.setLoginErrorCount(0);}Date date = new Date();SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String dataStr = format.format(date); //格式化日期try {thisErrorLoginTime = format.parse(dataStr);} catch (ParseException e) {e.printStackTrace();}if (islocked == 1) {Date lastLoginErrorTime = null;Long timeSlot = 0l;if (person.getLastLoginErrorTime() == null) {lastLoginErrorTime = thisErrorLoginTime;} else {lastLoginErrorTime = person.getLastLoginErrorTime();timeSlot = thisErrorLoginTime.getTime() - lastLoginErrorTime.getTime();}if (timeSlot < 1800000) {request.setAttribute("message", "您的账户已被锁定,请" + (30 - Math.ceil((double) timeSlot / 60000)) + "分钟之后再次尝试");try {Thread.currentThread().sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return "login";} else {//用户超过三十分钟,再次输错密码,解锁并且增加次数person.setLastLoginErrorTime(thisErrorLoginTime);//修改用户相当于更新一些信息personDao.updateByperson(thisErrorLoginTime, 1, 0, person.getPersonId());request.setAttribute("message", "账户或密码错误,您还有4次登陆机会");try {Thread.currentThread().sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return "login";}// 账户第五次登陆失败 ,此时登陆错误次数增加至5,以后错误仍是5,不再递增} else if (person.getLoginErrorCount() == 4) {person.setLoginErrorCount(5);person.setIsLocked(1);person.setLastLoginErrorTime(thisErrorLoginTime);//修改用户 更新用户信息personDao.updateByperson(thisErrorLoginTime, 5, 1, person.getPersonId());request.setAttribute("message", "您的账户已被锁定,请30分钟之后再次尝试登陆");try {Thread.currentThread().sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return "login";} else {// 账户前四次登陆失败person.setLoginErrorCount(person.getLoginErrorCount() + 1);person.setLastLoginErrorTime(thisErrorLoginTime);//修改用户 跟新用户信息personDao.updateByperson(thisErrorLoginTime, person.getLoginErrorCount(), person.getIsLocked(), person.getPersonId());request.setAttribute("message", "账户或密码错误,您还有" + (5 - person.getLoginErrorCount()) + "次登陆机会");try {Thread.currentThread().sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return "login";}} else {if (person.getIsLocked() == null) {person.setIsLocked(0);} else {islocked = person.getIsLocked();}if (islocked == 1) {// 最后一次登陆错误时间Date lastLoginErrorTime = null;Long timeSlot = 0L;if (person.getLastLoginErrorTime() == null) {lastLoginErrorTime = new Date();} else {lastLoginErrorTime = person.getLastLoginErrorTime();timeSlot = new Date().getTime() - lastLoginErrorTime.getTime();}if (timeSlot < 1800000) {// 判断最后锁定时间,30分钟之内继续锁定request.setAttribute("message", "您的账户已被锁定,请" + (30 - Math.ceil((double) timeSlot / 60000)) + "分钟之后再次尝试");try {Thread.currentThread().sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return "login";} else {// 判断最后锁定时间,30分钟之后登陆账户session.setAttribute("person", person);// 保存当前用户Date d = new Date();session.setAttribute("dateStr", d); // 保存当前用户登录时间用于显示person.setLoginErrorCount(0);person.setIsLocked(0);//修改用户表登录时间 跟新表格信息personDao.updateByperson(null, 0, 0, person.getPersonId());return "main";}} else {session.setAttribute("person", person);// 保存当前用户Date d = new Date();session.setAttribute("dateStr", d); // 保存当前用户登录时间用于显示person.setLoginErrorCount(0);person.setIsLocked(0);personDao.updateByperson(null, 0, 0, person.getPersonId());//修改用户表登录时间return "main";}}}return "login";---# 总结总结:主要借鉴了网上大佬们的逻辑实现,其实还有一个小小的问题,就是没有办法自动更新锁定的状态,过了三十分钟,自动把锁定给解除,得想个办法,写个定时任务?希望大佬给我解惑。

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