/**
* 发送验证码
* @param vo
* @return
* @throws Exception
*/
@RequestMapping(value = "/verificationCode", method = RequestMethod.POST)
public Result sendLoginPhoneCode(HttpServletRequest request,@RequestBody OpenRegisterVo vo) {
if (vo.getUser_phone() == null) {
return ResultGenerator.getFailResult("手机号不能为'null'!");
}
UserInfo userInfo = userInfoService.selectUserInfoByPhone(vo.getUser_phone());
if (userInfo == null) {
return ResultGenerator.getFailResult("请注册账号");
}
return verificationCode(request, vo);
}
@RequestMapping(value = "/verificationCode11", method = RequestMethod.POST)
public Result verificationCode(HttpServletRequest request,@RequestBody OpenRegisterVo vo){
if (ObjectUtils.isEmpty(vo) || StringUtils.isNull(vo.getUser_phone())){
return ResultGenerator.getFailResult("手机号不能为'null'!");
}
//取到登录用户的IP
String ipAddr = ServletUtil.getClientIP(request);
//手机号黑名单
String blackListPhone = BLACKLISTPHONECODE + vo.getUser_phone();
//ip黑名单
String blackListIp = BLACKLISTIPCODE + ipAddr;
//验证码
String key = BcsConstants.PHONE_MESSAGE_PREFIX + vo.getUser_phone();
//手机号累加值
String growKey = GROW + vo.getUser_phone();
//初始时间
String initTimeKey = INITIALCODE + vo.getUser_phone();
//ip累加值
String addrIp = ADDRIP + ipAddr;
//根据key从redis中查询黑名单
String blackPhonr = redisdbUtil.getVStr(blackListPhone, 0);
String blackIp = redisdbUtil.getVStr(blackListIp, 0);
if (blackIp != null && blackIp.equals("1")) {
return ResultGenerator.getFailResult("获取验证码过于频繁,IP已锁定,请30分钟后重新获取验证码");
}
if (blackPhonr != null && blackPhonr.equals("1")) {
return ResultGenerator.getFailResult("获取验证码过于频繁,账户已锁定,请30分钟后重新获取验证码");
}
// 获取随机验证码
vo.setAuth_code(SecurityUtil.getCode());
vo.setAppkey(appkey);
vo.setSecret(secret);
// 调用发短信的方法
SecurityUtil.sendCode(vo);
String strPhone = redisdbUtil.getIncr(growKey, 0);
Integer growPhone = (strPhone == null) ? 0 : Integer.valueOf(strPhone);
if (growPhone < 5) {
//每次调用发送短信方法后不论是否发送成功,都进行累加计数操作
redisdbUtil.setIncr(growKey, 1, 60 * 10);
}
String strAddrIp = redisdbUtil.getIncr(addrIp, 0);
Integer rAddrIpInt = (strAddrIp == null) ? 0 : Integer.valueOf(strAddrIp);
if (rAddrIpInt < 10) {
//统计ip的访问次数
redisdbUtil.setIncr(addrIp, 1, 60 * 10);
}
// 1.取到当前时间戳,取到5分钟后的时间戳
Map<String, String> initMap = redisdbUtil.hmGetAll(initTimeKey, 0);
Long initialTime = initMap.get("initialTime") == null ? 0 : Long.valueOf(initMap.get("initialTime"));
long nowTime = System.currentTimeMillis();
long afterTime = initialTime + 300000;
//2.如果在5分钟范围内,点击获取验证码超过5次,将手机号加入redis黑名单
if (nowTime > initialTime && nowTime < afterTime) {
if (growPhone >= 4) {
// 设置过期时间 12小时
redisdbUtil.set(blackListPhone, "1", 60 * 30);
redisdbUtil.clearData(growKey);
}
if (rAddrIpInt >= 9) {
redisdbUtil.set(blackListIp, "1", 60 * 30);
redisdbUtil.clearData(addrIp);
}
}
if (growPhone == 0 || nowTime > afterTime) {
//第一次调用后将初始时间存储到redis中
Map<String, String> mapVal = new HashMap<>();
mapVal.put(INITIALTIME, System.currentTimeMillis() + "");
redisdbUtil.hmsetData(initTimeKey, mapVal, 0, 60 * 10);
}
// 将验证码存入redis
Map<String, String> map = new HashMap<>();
map.put(BcsConstants.PHONE_MESSAGE_FIELD_CODE, vo.getAuth_code());
// 设置过期时间 默认5分钟
redisdbUtil.hmsetData(key, map, 0, 60 * 5);
LOGGER.info("key[" + key + "]:save redis success!");
return ResultGenerator.getSuccessResult();
}
发送验证码
原创
©著作权归作者所有:来自51CTO博客作者mb655c6061e1946的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
生成验证码-超简单
轻松应对验证挑战:简单验证码的实现方法!
验证码 Java -
Python发送验证码短信
Python、urllib、验证码短信
python urllib 验证码 Python 用户名 -
注册+发送验证码思路
文章目录图思路关键代码图思路点击页面上的”获取短信验证码”连接、
品优购 注册发送验证码 redis 验证码 短信验证码