一 用户输入错误验证码错误三次后,锁定该用户3分钟 redis 使用案列
@ResponseBody
@RequestMapping("sendCode")
public String sendCode(String tel){
int code= (int) (Math.random()*999999);
// 存储60秒
int time=60;
redisTemplate.opsForValue().set(tel,code+"",time, TimeUnit.SECONDS);
System.out.println("电话:"+tel+"验证码:"+code);
// 这里采用的工具类发送手机
// String send = SendTeleMsgUtil.send(tel, code + "", time);
return code+"";
}
@PostMapping("login")
public String login(String tel,String code){
// 判断手机号
Boolean b = redisTemplate.hasKey(tel);
// 固定常量
String count="_LOGIN_COUNT";
String user_lock="_USER_LOCK";
if (b){
// 判断验证码是否正确
if (code.equals(redisTemplate.opsForValue().get(tel))){
// 判断该用户是否被锁定
if (!redisTemplate.hasKey(user_lock)){
// 不存在则放行
return "redirect:list";
}
}else {
// 失败记录次数 第一次设置 值为1 存在60秒 与验证码存在时间同步
Boolean first = redisTemplate.opsForValue().setIfAbsent(tel+count, 1,60,TimeUnit.SECONDS);
if (!first){
// 第二次自增 1
redisTemplate.opsForValue().increment(tel+count);
if ((int)redisTemplate.opsForValue().get(tel+count) > 3){
// 失败超过3次 锁定用户 3分钟
redisTemplate.opsForValue().set(tel+user_lock,1,3,TimeUnit.MINUTES);
System.out.println("已上锁");
}
}
}
}
// 登陆失败
//System.out.println(redisTemplate.opsForValue().get(tel+count)+"------------------------");
return "login";
}