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

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


前言

工作的第一天,就接到了一个登录锁定与解锁的需求,第一反应,这是什么,我要怎么做,怎么上来就让我写这么难的业务,哈哈。不过万物皆可百度,经过观看网上大佬们的实现方法,经过本人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";

---

# 总结
总结:主要借鉴了网上大佬们的逻辑实现,其实还有一个小小的问题,就是没有办法自动更新锁定的状态,过了三十分钟,自动把锁定给解除,得想个办法,写个定时任务?希望大佬给我解惑。
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值