From 90b030448b9cb9e531dd525a68576403594f8e78 Mon Sep 17 00:00:00 2001 From: chendengyao <1171077218@qq.com> Date: Thu, 17 Jun 2021 16:41:54 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=99=BB=E9=99=86=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E6=AC=A1=E6=95=B0=E8=BF=87=E5=A4=9A=E6=97=B6=E9=94=81?= =?UTF-8?q?=E5=AE=9A=E8=B4=A6=E6=88=B730=E5=88=86=E9=92=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liuwanr/service/UserInfoService.java | 33 +++++++++++++++++-- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/yipin/liuwanr/service/UserInfoService.java b/src/main/java/com/yipin/liuwanr/service/UserInfoService.java index 2baad86..fb48cc2 100644 --- a/src/main/java/com/yipin/liuwanr/service/UserInfoService.java +++ b/src/main/java/com/yipin/liuwanr/service/UserInfoService.java @@ -397,7 +397,15 @@ public class UserInfoService { } //登陆获取学校/电子科大 public HashMap loginSchoolClient(String account,String password,Integer schoolId,Integer source){ + ValueOperations redis = stringRedisTemplate.opsForValue(); HashMap resp = new HashMap(); + //用户登录失败次数 + String userLoginNum = redis.get(account + "LoginNum"); + if (userLoginNum != null && Integer.valueOf(userLoginNum) > 4) { + resp.put("retcode", 500); + resp.put("retvalue", "登录失败次数过多,账户已锁定!"); + return resp; + } HashMap obj = new HashMap(); UserInfo userInfo = new UserInfo(); userInfo.setPassword(MD5.encrypt(password)); @@ -441,8 +449,9 @@ public class UserInfoService { List studentList = userInfoMapper.loginsGetStudentSchoolInfo(userId,schoolId); obj.put("studentList",studentList); + //登录成功删除redis登录失败次数 + redis.getOperations().delete(account + "LoginNum"); //鉴权,登录后将信息存入redis - ValueOperations redis = stringRedisTemplate.opsForValue(); String loginToken = UUID.randomUUID().toString(); redis.set(loginToken,user.getUserId().toString(),2, TimeUnit.HOURS); obj.put("loginToken",loginToken); @@ -450,6 +459,11 @@ public class UserInfoService { resp.put("retcode", 200); resp.put("retvalue", obj); }else { + //记录登录失败次数30分钟 + int loginNum = 1; + if (userLoginNum != null) loginNum = loginNum + Integer.valueOf(userLoginNum); + redis.set(account+"LoginNum", String.valueOf(loginNum), 30,TimeUnit.MINUTES); + resp.put("retcode", 300); resp.put("retvalue", "登录失败,密码有误!"); } @@ -506,7 +520,15 @@ public class UserInfoService { //登陆 public HashMap adminLogins(String account,String password,Integer source){ + ValueOperations redis = stringRedisTemplate.opsForValue(); HashMap resp = new HashMap(); + //用户登录失败次数 + String userLoginNum = redis.get(account + "LoginNum"); + if (userLoginNum != null && Integer.valueOf(userLoginNum) > 4) { + resp.put("retcode", 500); + resp.put("retvalue", "登录失败次数过多,账户已锁定!"); + return resp; + } UserInfo user = new UserInfo(); user.setPassword(MD5.encrypt(password)); try { @@ -535,6 +557,11 @@ public class UserInfoService { } } if (user == null){ + //记录登录失败次数30分钟 + int num = 1; + if (userLoginNum != null) num = num + Integer.valueOf(userLoginNum); + redis.set(account+"LoginNum", String.valueOf(num), 30,TimeUnit.MINUTES); + resp.put("retvalue","账号密码有误,请稍后再试!"); resp.put("retcode", 300); }else{ @@ -544,10 +571,10 @@ public class UserInfoService { //登录成功,重新设置最后登录时间以及登录次数 userInfoMapper.updateLogInNumber(user); String token = user.getToken(); - ValueOperations redis = stringRedisTemplate.opsForValue(); redis.set(token,lastLoginTime, 24, TimeUnit.HOURS); user.setDataTime(lastLoginTime); - + //登录成功删除redis登录失败次数 + redis.getOperations().delete(account + "LoginNum"); //鉴权,登录后将信息存入redis String loginToken = UUID.randomUUID().toString(); redis.set(loginToken,user.getUserId().toString(),2, TimeUnit.HOURS);