Springboot所学所写SpringBoot极简教程 · Spring Boot

手机验证码校验

2019-05-10  本文已影响0人  织梦少年666

后台管理员通过手机号获取验证码登陆,每天只有3次机会

直接上代码

获取短信验证码,先判断该手机号是否在数据库中,是否禁用状态,发送成功后存入redis,过期时间为5分钟,保证只有最新的验证码是有效的
@PostMapping("/open/getVerification")
    @ApiOperation(value = "获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String", paramType = "query"),
    })
    public Results.Result getVerification(String phone) {
        AgencyProjectExample agencyProjectExample = new AgencyProjectExample();
        agencyProjectExample.createCriteria().andPhoneEqualTo(phone);
        List<AgencyProject> agencyProject = agencyProjectService.selectByExample(agencyProjectExample);
        if (agencyProject == null) {
            return Results.AGENCY_PROJECT_MORE;
        } else {
            if(agencyProject.get(0).getState() == 0){
                return new Results.Result(500,"您的账号已被禁用");
            }
            String code = VerifyCodeUtils.generateVerifyNumber(6);
            int i = YunPianSendMsg.sendCaptcha(code, phone, 5);
            if (i == -1) {
                return Results.AGENCY_PROJECT_SEND;
            } else {
                redisService.delete(phone);//重新获取验证码得先删除之前的验证码信息 在添加 只能保证只有最新的验证码有效
                redisService.save(phone, code, 300);//获取的验证码存入redis 过期时间5分钟
                return Results.SUCCESS;
            }
        }
    }
默认登陆错误次数是0,每次错误+1,过期时间为12小时,当次数大于等于3时,提示账号错误次数过多,请明天尝试。每次错误会提示,您还剩下几次登陆机会
@PostMapping("/open/projectLogin")
    @ApiOperation(value = "项目方登陆")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "短信验证码", required = true, dataType = "String", paramType = "query")
    })
    public Results.Result projectLogin(String phone, String code) {
        if (phone == null || code == null) {
            return Results.PARAMETER_INCORRENT;
        }
        String key = RedisKeyUtil.projectErrorLoginOfPhone(phone);
        Object obj = redisService.select(key);
        if (obj == null) {
            obj = "0";
        }
        int errorCount = Integer.parseInt(obj.toString());
        if (errorCount >= 3) {
            return Results.AGENCY_PROJECT_ERROR;
        }
        AgencyProjectExample agencyProjectExample = new AgencyProjectExample();
        agencyProjectExample.createCriteria().andPhoneEqualTo(phone);
        List<AgencyProject> agencyProject = agencyProjectService.selectByExample(agencyProjectExample);
        int count = 2 - errorCount;
        if (agencyProject == null) {
            if(count == 0){
                return Results.AGENCY_PROJECT_ERROR;
            }
            redisService.save(key,++errorCount,60 * 60 * 12);
            return new Results.Result(500,"手机号错误,您今天还剩"+count+"机会");
        } else {
            if(agencyProject.get(0).getState() == 0){
                return new Results.Result(500,"您的账号已被禁用");
            }
            String redisCode = redisService.select(phone);//redis存入的验证码
            if (redisCode.equals(code)) {
                return Results.SUCCESS;
            } else {
                if(count == 0){
                    return Results.AGENCY_PROJECT_ERROR;
                }
                redisService.save(key,++errorCount,60 * 60 * 12);
                return new Results.Result(500,"验证错误,您今天还剩"+count+"机会");
            }
        }
    }
上一篇下一篇

猜你喜欢

热点阅读