1. 程式人生 > >取redis中手機驗證碼,並驗證是否正確

取redis中手機驗證碼,並驗證是否正確

controller:

/**
 * 校驗驗證碼是否正確
 * @param smsCode
 * @param phone
 */
@RequestMapping(value = "checkSmsCode",method = RequestMethod.POST)
public Result checkSmsCode(String smsCode, String phone) {
    return authService.checkSmsCode(smsCode, phone);
}

service:

/**
 * 校驗驗證碼是否正確
 *
 * @param smsCode
 * @param phone
 */
public Result checkSmsCode(String smsCode, String phone) {
    Result result = new Result();
    if (redisTemplate.opsForValue().get(phone) == null) {
        result.setStatus(1);
        result.setMsg("簡訊驗證碼輸入超時!");
    } else {
        String code = redisTemplate.opsForValue().get(phone).toString();
        if (!code.equals(smsCode)) {
            result.setStatus(2);
            result.setMsg(簡訊驗證碼錯誤!");
        } else {
            result.setStatus(0);
             result.setMsg("成功!");
        }

    }
    return result;
}