手机号验证码次数限制
This commit is contained in:
parent
3ebb67dd24
commit
dd31e09009
|
|
@ -94,6 +94,14 @@ public class TokenController {
|
||||||
|
|
||||||
@PostMapping("sendCode")
|
@PostMapping("sendCode")
|
||||||
public R<?> sendCode(@RequestBody LoginBody form) {
|
public R<?> sendCode(@RequestBody LoginBody form) {
|
||||||
|
String uuid = form.getUuid();
|
||||||
|
String captcha = redisService.getCacheObject(CacheConstants.CAPTCHA_CODE_KEY + uuid).toString();
|
||||||
|
if (StringUtils.isBlank(captcha)) {
|
||||||
|
return R.fail("验证码超时,请重新刷新");
|
||||||
|
}
|
||||||
|
if (form.getCode() != null && !form.getCode().equals(captcha)) {
|
||||||
|
return R.fail("验证码错误");
|
||||||
|
}
|
||||||
R<Boolean> sendState = remoteUserService.sendCode(form.getPhone());
|
R<Boolean> sendState = remoteUserService.sendCode(form.getPhone());
|
||||||
return sendState;
|
return sendState;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -87,4 +87,9 @@ public class UserConstants
|
||||||
* 手机号长度限制
|
* 手机号长度限制
|
||||||
*/
|
*/
|
||||||
public static final int PHONE_DEFAULT_LENGTH_LOGIN = 11;
|
public static final int PHONE_DEFAULT_LENGTH_LOGIN = 11;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 手机1天限制
|
||||||
|
*/
|
||||||
|
public static final String MOBILE_PHONE_1D_LIMIT_DIR = "mobilePhoneDay:";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -136,6 +136,10 @@ public class SysSmsServiceImpl implements ISysSmsService {
|
||||||
if (phone == null || phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) {
|
if (phone == null || phone.length() != UserConstants.PHONE_DEFAULT_LENGTH_LOGIN) {
|
||||||
return AjaxResult.error("手机号格式错误,请输入11位数字号码");
|
return AjaxResult.error("手机号格式错误,请输入11位数字号码");
|
||||||
}
|
}
|
||||||
|
// 检查发送次数是否超限
|
||||||
|
if (isOverLimit(phone)) {
|
||||||
|
throw new ServiceException("当天此手机号发送验证码次数超过限制", 1001);
|
||||||
|
}
|
||||||
String code = getSixBitCode();
|
String code = getSixBitCode();
|
||||||
// 校验验证码
|
// 校验验证码
|
||||||
if (code.length() != UserConstants.CODE_MIN_LENGTH_LOGIN) {
|
if (code.length() != UserConstants.CODE_MIN_LENGTH_LOGIN) {
|
||||||
|
|
@ -151,12 +155,33 @@ public class SysSmsServiceImpl implements ISysSmsService {
|
||||||
}
|
}
|
||||||
// 存储验证码至Redis中,键值为:code_15588886157 , 有效期5,时间颗粒度为MINUTES:分钟
|
// 存储验证码至Redis中,键值为:code_15588886157 , 有效期5,时间颗粒度为MINUTES:分钟
|
||||||
redisService.setCacheObject("code_" + phone, code, 5L, TimeUnit.MINUTES);
|
redisService.setCacheObject("code_" + phone, code, 5L, TimeUnit.MINUTES);
|
||||||
return success("手机号:" + phone + ",用户登录验证码:" + code + ",返回结果:" + body);
|
String key = UserConstants.MOBILE_PHONE_1D_LIMIT_DIR + phone;
|
||||||
|
Integer sendCount = redisService.getCacheObject(key);
|
||||||
|
if (sendCount == null) {
|
||||||
|
sendCount = 1;
|
||||||
|
} else {
|
||||||
|
sendCount++;
|
||||||
|
}
|
||||||
|
//存储一天手机号发送验证码数据
|
||||||
|
redisService.setCacheObject(key, sendCount, 1L, TimeUnit.DAYS);
|
||||||
|
return success("手机号:" + phone + ",短信验证码发送成功 !");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return AjaxResult.error("发送失败:" + e.getMessage());
|
return AjaxResult.error("发送失败:" + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查发送次数是否超过限制
|
||||||
|
* @param phone 手机号
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
private boolean isOverLimit(String phone) {
|
||||||
|
String key = UserConstants.MOBILE_PHONE_1D_LIMIT_DIR + phone;
|
||||||
|
Integer sendCount = redisService.getCacheObject(key);
|
||||||
|
// 限制每个手机号每天最多发送10次
|
||||||
|
return sendCount != null && sendCount >= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断验证码是否存在Redis中
|
* 判断验证码是否存在Redis中
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue