登录-密码错误处理功能
设置Redis过期时间为凌晨
1 | /** |
2 | * 判断当前时间距离第二天凌晨的秒数 |
3 | * |
4 | * @return 返回值单位为[s:秒] |
5 | */ |
6 | public static Integer getSecondsNextEarlyMorning() { |
7 | Calendar cal = Calendar.getInstance(); |
8 | cal.add(Calendar.DAY_OF_YEAR, 1); |
9 | cal.set(Calendar.HOUR_OF_DAY, 0); |
10 | cal.set(Calendar.SECOND, 0); |
11 | cal.set(Calendar.MINUTE, 0); |
12 | cal.set(Calendar.MILLISECOND, 0); |
13 | return Math.toIntExact((cal.getTimeInMillis() - System.currentTimeMillis()) / 1000); |
14 | } |
用户密码输入错误
连续错误5次一小时后登陆
连续错误10次当天不能登陆
连续错误20次要求用户更改密码
1 | /** |
2 | * 校验密码,类计错误5次锁定1小时,累计错误10次当天不能登录,累计错误20次锁定账号 需修改密码 |
3 | * @param pwdDB 数据库中密码 |
4 | * @param lgpwd 用户输入密码 |
5 | * @param lgmobile 用户手机号码 |
6 | * @return void |
7 | */ |
8 | public void checkPassword(String pwdDB, String lgpwd, String lgmobile) { |
9 | String keys = "login-count" + lgmobile;//用户密码错误次数 |
10 | String lockHour = "lockHour-hour" + lgmobile;//用户锁定1小时 |
11 | String lockDay = "lockHour-day" + lgmobile;//用户锁定1天 |
12 | String lock = "lock" + lgmobile;//用户密码错误次数达到20次 锁定 |
13 | //查询用户是否已锁定 提示修改密码 |
14 | if (redis.get(lock) != null && "LOCK".equals(redis.get(lock))) { |
15 | Locale local = CurrentUtils.getCurrentLocale(); |
16 | Integer key = ReturnCode.ERROR_64435.getCode(); |
17 | String message = i18nService.getMessage(key.toString(), null, local); |
18 | ResultUtil.throwExcepion(ResultUtil.createFail(ReturnCode.ERROR_64435.getCode(), message)); |
19 | } |
20 | |
21 | //查询用户是否当天被锁定 |
22 | if (redis.get(lockDay) != null && "LOCK_DAY".equals(redis.get(lockDay))) { |
23 | Locale local = CurrentUtils.getCurrentLocale(); |
24 | Integer key = ReturnCode.ERROR_64434.getCode(); |
25 | String message = i18nService.getMessage(key.toString(), null, local); |
26 | ResultUtil.throwExcepion(ResultUtil.createFail(ReturnCode.ERROR_64434.getCode(), message)); |
27 | } |
28 | |
29 | //查询用户是否被锁定一小时 |
30 | if (redis.get(lockHour) != null && "LOCK_HOUR".equals(redis.get(lockHour))) { |
31 | Locale local = CurrentUtils.getCurrentLocale(); |
32 | Integer key = ReturnCode.ERROR_64433.getCode(); |
33 | String message = i18nService.getMessage(key.toString(), null, local); |
34 | ResultUtil.throwExcepion(ResultUtil.createFail(ReturnCode.ERROR_64433.getCode(), message)); |
35 | } |
36 | //获取用户密码错误次数 |
37 | String missCount = redis.get(keys); |
38 | if (missCount == null) { |
39 | missCount = String.valueOf(1); |
40 | redis.set(keys, missCount); |
41 | } |
42 | //判断用户密码错误次数 |
43 | Integer count = Integer.parseInt(missCount); |
44 | if (count < 5) { |
45 | if (!pwdDB.equals(DigestUtils.md5Hex(lgpwd))) { |
46 | System.out.println("密码输入错误" + count + "次"); |
47 | String msg = "密码输入错误" + count + "次"; |
48 | //密码输入错误计数器加1 |
49 | count++; |
50 | redis.set(keys, String.valueOf(count)); |
51 | //设置密码计数过期时间 凌晨过期 |
52 | //redis.expire(keys, DateUtil.getSecondsNextEarlyMorning()); |
53 | ResultUtil.throwExcepion(ResultUtil.createFail(200, msg)); |
54 | } else { |
55 | //密码正确 清除错误次数 |
56 | redis.del(keys); |
57 | } |
58 | } |
59 | if (count == 5) { |
60 | //锁定1小时 |
61 | count++; |
62 | redis.set(keys, String.valueOf(count)); |
63 | redis.set(lockHour, "LOCK_HOUR"); |
64 | redis.expire(lockHour, 3600); |
65 | Locale local = CurrentUtils.getCurrentLocale(); |
66 | Integer key = ReturnCode.ERROR_64433.getCode(); |
67 | String message = i18nService.getMessage(key.toString(), null, local); |
68 | ResultUtil.throwExcepion(ResultUtil.createFail(ReturnCode.ERROR_64433.getCode(), message)); |
69 | } |
70 | if (count > 5 && count < 10) { |
71 | if (!pwdDB.equals(DigestUtils.md5Hex(lgpwd))) { |
72 | System.out.println("密码输入错误" + count + "次"); |
73 | String msg = "密码输入错误" + count + "次"; |
74 | //密码输入错误计数器加1 |
75 | count++; |
76 | redis.set(keys, String.valueOf(count)); |
77 | ResultUtil.throwExcepion(ResultUtil.createFail(200, msg)); |
78 | } else { |
79 | //密码正确 清除错误次数 |
80 | redis.del(keys); |
81 | } |
82 | } |
83 | if (count == 10) { |
84 | //锁定1天 |
85 | count++; |
86 | redis.set(keys, String.valueOf(count)); |
87 | redis.set(lockDay, "LOCK_DAY"); |
88 | //设置过期时间 凌晨过期 |
89 | redis.expire(lockDay, DateUtil.getSecondsNextEarlyMorning()); |
90 | Locale local = CurrentUtils.getCurrentLocale(); |
91 | Integer key = ReturnCode.ERROR_64434.getCode(); |
92 | String message = i18nService.getMessage(key.toString(), null, local); |
93 | ResultUtil.throwExcepion(ResultUtil.createFail(ReturnCode.ERROR_64434.getCode(), message)); |
94 | } |
95 | if (count > 10 && count < 20) { |
96 | if (!pwdDB.equals(DigestUtils.md5Hex(lgpwd))) { |
97 | System.out.println("密码输入错误" + count + "次"); |
98 | String msg = "密码输入错误" + count + "次"; |
99 | //密码输入错误计数器加1 |
100 | count++; |
101 | redis.set(keys, String.valueOf(count)); |
102 | ResultUtil.throwExcepion(ResultUtil.createFail(200, msg)); |
103 | } else { |
104 | //密码正确 清除错误次数 |
105 | redis.del(keys); |
106 | } |
107 | } |
108 | if (count == 20) { |
109 | redis.set(lock, "LOCK"); |
110 | Locale local = CurrentUtils.getCurrentLocale(); |
111 | Integer key = ReturnCode.ERROR_64435.getCode(); |
112 | String message = i18nService.getMessage(key.toString(), null, local); |
113 | ResultUtil.throwExcepion(ResultUtil.createFail(ReturnCode.ERROR_64435.getCode(), message)); |
114 | } |
115 | } |
返回错误码
1 | ERROR_64433(64433,"密码输入错误次数达到5次,请您1小时后重试"), |
2 | ERROR_64434(64434,"密码输入错误次数达到10次,请您明天重试"), |
3 | ERROR_64435(64435,"密码输入错误次数达到20次,请更改您的密码"); |