张延森 преди 4 години
родител
ревизия
927b11416d

+ 1
- 1
pom.xml Целия файл

@@ -10,7 +10,7 @@
10 10
 	</parent>
11 11
 	<groupId>com.huiju</groupId>
12 12
 	<artifactId>xiangsong</artifactId>
13
-	<version>1.0.53</version>
13
+	<version>1.0.54</version>
14 14
 	<name>xiangsong</name>
15 15
 	<description>香颂</description>
16 16
 

+ 23
- 1
src/main/java/com/huiju/estateagents/center/taUser/controller/TaUserController.java Целия файл

@@ -18,6 +18,7 @@ import com.huiju.estateagents.service.*;
18 18
 import io.swagger.annotations.ApiImplicitParam;
19 19
 import io.swagger.annotations.ApiImplicitParams;
20 20
 import io.swagger.annotations.ApiOperation;
21
+import org.checkerframework.checker.units.qual.A;
21 22
 import org.slf4j.Logger;
22 23
 import org.slf4j.LoggerFactory;
23 24
 import org.springframework.beans.factory.annotation.Autowired;
@@ -26,6 +27,7 @@ import org.springframework.web.bind.annotation.*;
26 27
 import javax.servlet.http.HttpServletRequest;
27 28
 
28 29
 import java.text.SimpleDateFormat;
30
+import java.time.Duration;
29 31
 import java.time.LocalDateTime;
30 32
 import java.util.*;
31 33
 
@@ -85,6 +87,9 @@ public class TaUserController extends BaseController {
85 87
     @Autowired
86 88
     public CaptchaUtils captchaUtils;
87 89
 
90
+    @Autowired
91
+    LoginLocker loginLocker;
92
+
88 93
     /**
89 94
      * 分页查询列表
90 95
      * @param pageNum
@@ -342,6 +347,18 @@ public class TaUserController extends BaseController {
342 347
         String captcha = params.getString("captcha");
343 348
         String captchaKey = params.getString("captchaKey");
344 349
 
350
+        // 试错 3 次被锁定
351
+        int maxTryTimes = 3;
352
+        if (null != userName) {
353
+            LoginLocker.LogInfo logInfo = loginLocker.get("admin-" + userName);
354
+            if (null != logInfo) {
355
+                if (logInfo.getTimes() >= maxTryTimes) {
356
+                    String errMsg = String.format("该账号被锁定, 请 %d 分钟后重试", Duration.between(LocalDateTime.now(), logInfo.getExpire()).toMinutes() + 1);
357
+                    return ResponseBean.error(errMsg, ResponseBean.ERROR_UNAVAILABLE);
358
+                }
359
+            }
360
+        }
361
+
345 362
         if (!captchaUtils.valid(captchaKey, captcha)) {
346 363
             return ResponseBean.error("验证码不正确或者已过期", ResponseBean.ERROR_ILLEGAL_PARAMS);
347 364
         }
@@ -352,9 +369,14 @@ public class TaUserController extends BaseController {
352 369
         sysUserQueryWrapper.eq("login_password", MD5Utils.md5(userPassword));
353 370
         TaUser taUser = iTaUserService.getOne(sysUserQueryWrapper);
354 371
         if (taUser == null) {
355
-            return ResponseBean.error("用户名或密码错误", ResponseBean.ERROR_ILLEGAL_PARAMS);
372
+            LoginLocker.LogInfo logInfo = loginLocker.add("admin-" + userName);
373
+            String errMsg = String.format("用户名或密码错误, 您还有 %d 次机会", maxTryTimes - logInfo.getTimes());
374
+            return ResponseBean.error(errMsg, ResponseBean.ERROR_ILLEGAL_PARAMS);
356 375
         }
357 376
 
377
+        // 解除锁定
378
+        loginLocker.remove("admin-"+userName);
379
+
358 380
         QueryWrapper<TaUser> taUserQueryWrapper = new QueryWrapper<>();
359 381
         taUserQueryWrapper.eq("org_id", taUser.getOrgId());
360 382
         taUserQueryWrapper.eq("is_admin", 1);

+ 1
- 2
src/main/java/com/huiju/estateagents/common/CaptchaUtils.java Целия файл

@@ -10,7 +10,6 @@ import org.springframework.stereotype.Component;
10 10
 
11 11
 import java.time.LocalDateTime;
12 12
 import java.util.Hashtable;
13
-import java.util.Map;
14 13
 import java.util.UUID;
15 14
 
16 15
 @Slf4j
@@ -70,7 +69,7 @@ public class CaptchaUtils {
70 69
     }
71 70
 
72 71
     /**
73
-     * 定时任务 每半小时 清除一次不用的手机号
72
+     * 定时任务 每半小时 清除一次不用的缓存
74 73
      */
75 74
     @Scheduled(fixedRate = 1000 * 60 * 30)
76 75
     public void clearUnused() {

+ 75
- 0
src/main/java/com/huiju/estateagents/common/LoginLocker.java Целия файл

@@ -0,0 +1,75 @@
1
+package com.huiju.estateagents.common;
2
+
3
+import lombok.Data;
4
+import lombok.experimental.Accessors;
5
+import lombok.extern.slf4j.Slf4j;
6
+import org.springframework.scheduling.annotation.EnableScheduling;
7
+import org.springframework.scheduling.annotation.Scheduled;
8
+import org.springframework.stereotype.Component;
9
+
10
+import java.time.LocalDateTime;
11
+import java.util.Hashtable;
12
+
13
+@Slf4j
14
+@Component
15
+@EnableScheduling
16
+public class LoginLocker {
17
+    // 锁定时长 分钟
18
+    public static final int lockTime = 5;
19
+
20
+    Hashtable<String, LogInfo> allLogs = new Hashtable<>();
21
+
22
+    public LogInfo get(String name) {
23
+        return allLogs.get(name);
24
+    }
25
+
26
+    public LogInfo add(String name) {
27
+        if (null == name) {
28
+            return null;
29
+        }
30
+
31
+        LogInfo logInfo = allLogs.get(name);
32
+        if (null == logInfo) {
33
+            logInfo = new LogInfo()
34
+                    .setKey(name)
35
+                    .setTimes(1)
36
+                    .setExpire(LocalDateTime.now().plusMinutes(lockTime));
37
+            allLogs.put(name, logInfo);
38
+        } else {
39
+            logInfo.setTimes(logInfo.getTimes() + 1)
40
+                    .setExpire(LocalDateTime.now().plusMinutes(lockTime));
41
+        }
42
+
43
+        return logInfo;
44
+    }
45
+
46
+    public void remove(String name) {
47
+        try {
48
+            allLogs.remove(name);
49
+        } catch (Exception e) {}
50
+    }
51
+
52
+    private boolean checkExpire(LogInfo log) {
53
+        return LocalDateTime.now().isAfter(log.getExpire());
54
+    }
55
+
56
+    /**
57
+     * 定时任务 每分钟 清除一次不用的缓存
58
+     */
59
+    @Scheduled(fixedRate = 1000 * 60)
60
+    public void clearUnused() {
61
+        for (LogInfo log: allLogs.values()) {
62
+            if (checkExpire(log)) {
63
+                allLogs.remove(log.getKey());
64
+            }
65
+        }
66
+    }
67
+
68
+    @Data
69
+    @Accessors(chain = true)
70
+    public static class LogInfo {
71
+        String key;
72
+        int times;
73
+        LocalDateTime expire;
74
+    }
75
+}