张延森 4 年 前
コミット
72cba74b13

+ 6
- 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.52</version>
13
+	<version>1.0.53</version>
14 14
 	<name>xiangsong</name>
15 15
 	<description>香颂</description>
16 16
 
@@ -151,6 +151,11 @@
151 151
             <artifactId>spring-boot-starter-aop</artifactId>
152 152
         </dependency>
153 153
 
154
+		<dependency>
155
+			<groupId>com.github.whvcse</groupId>
156
+			<artifactId>easy-captcha</artifactId>
157
+			<version>1.6.2</version>
158
+		</dependency>
154 159
 
155 160
 		<dependency>
156 161
 			<groupId>fadada</groupId>

+ 11
- 5
src/main/java/com/huiju/estateagents/center/taUser/controller/TaUserController.java ファイルの表示

@@ -11,10 +11,7 @@ import com.huiju.estateagents.center.sysUser.entity.SysToken;
11 11
 import com.huiju.estateagents.center.sysUser.service.ISysTokenService;
12 12
 import com.huiju.estateagents.center.taUser.entity.*;
13 13
 import com.huiju.estateagents.center.taUser.service.*;
14
-import com.huiju.estateagents.common.CommConstant;
15
-import com.huiju.estateagents.common.JWTUtils;
16
-import com.huiju.estateagents.common.MD5Utils;
17
-import com.huiju.estateagents.common.StringUtils;
14
+import com.huiju.estateagents.common.*;
18 15
 import com.huiju.estateagents.entity.*;
19 16
 import com.huiju.estateagents.mapper.TaRoleMapper;
20 17
 import com.huiju.estateagents.service.*;
@@ -85,6 +82,9 @@ public class TaUserController extends BaseController {
85 82
     @Autowired
86 83
     public ITpLifeConsultantEvaluateService iTpLifeConsultantEvaluateService;
87 84
 
85
+    @Autowired
86
+    public CaptchaUtils captchaUtils;
87
+
88 88
     /**
89 89
      * 分页查询列表
90 90
      * @param pageNum
@@ -339,7 +339,13 @@ public class TaUserController extends BaseController {
339 339
     
340 340
         String userName = params.getString("loginName");
341 341
         String userPassword = params.getString("loginPassword");
342
-    
342
+        String captcha = params.getString("captcha");
343
+        String captchaKey = params.getString("captchaKey");
344
+
345
+        if (!captchaUtils.valid(captchaKey, captcha)) {
346
+            return ResponseBean.error("验证码不正确或者已过期", ResponseBean.ERROR_ILLEGAL_PARAMS);
347
+        }
348
+
343 349
         //验证用户名密码是否正确
344 350
         QueryWrapper<TaUser> sysUserQueryWrapper = new QueryWrapper<>();
345 351
         sysUserQueryWrapper.eq("login_name",userName);

+ 92
- 0
src/main/java/com/huiju/estateagents/common/CaptchaUtils.java ファイルの表示

@@ -0,0 +1,92 @@
1
+package com.huiju.estateagents.common;
2
+
3
+import com.wf.captcha.SpecCaptcha;
4
+import lombok.Data;
5
+import lombok.experimental.Accessors;
6
+import lombok.extern.slf4j.Slf4j;
7
+import org.springframework.scheduling.annotation.EnableScheduling;
8
+import org.springframework.scheduling.annotation.Scheduled;
9
+import org.springframework.stereotype.Component;
10
+
11
+import java.time.LocalDateTime;
12
+import java.util.Hashtable;
13
+import java.util.Map;
14
+import java.util.UUID;
15
+
16
+@Slf4j
17
+@Component
18
+@EnableScheduling
19
+public class CaptchaUtils {
20
+    Hashtable<String, Captcha> allCaptchas = new Hashtable<>();
21
+
22
+    public Captcha generate() {
23
+        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
24
+        Captcha cap = new Captcha()
25
+                .setKey(UUID.randomUUID().toString())
26
+                .setValue(specCaptcha.text().toLowerCase())
27
+                .setImage(specCaptcha.toBase64())
28
+                .setExpire(LocalDateTime.now().plusMinutes(15));
29
+        allCaptchas.put(cap.getKey(), cap);
30
+
31
+        // 返回的内容不包含 value
32
+        return copy(cap).setValue(null);
33
+    }
34
+
35
+    public boolean valid(String key, String captcha) {
36
+        if (null == captcha || null == key) {
37
+            return false;
38
+        }
39
+
40
+        // 没有对应键值
41
+        Captcha cap = allCaptchas.get(key);
42
+        if (null == cap) {
43
+            return false;
44
+        }
45
+
46
+        // 内容不匹配
47
+        if (!cap.getValue().equals(captcha.toLowerCase())) {
48
+            return false;
49
+        }
50
+
51
+        boolean res = checkExpire(cap);
52
+        if (res) {
53
+            // 过期清除
54
+            allCaptchas.remove(key);
55
+        }
56
+
57
+        return !res;
58
+    }
59
+
60
+    private Captcha copy(Captcha captcha) {
61
+        return new Captcha()
62
+                .setKey(captcha.getKey())
63
+                .setValue(captcha.getValue())
64
+                .setImage(captcha.getImage())
65
+                .setExpire(captcha.getExpire());
66
+    }
67
+
68
+    private boolean checkExpire(Captcha cap) {
69
+        return LocalDateTime.now().isAfter(cap.getExpire());
70
+    }
71
+
72
+    /**
73
+     * 定时任务 每半小时 清除一次不用的手机号
74
+     */
75
+    @Scheduled(fixedRate = 1000 * 60 * 30)
76
+    public void clearUnused() {
77
+        for (Captcha cap: allCaptchas.values()) {
78
+            if (checkExpire(cap)) {
79
+                allCaptchas.remove(cap.getKey());
80
+            }
81
+        }
82
+    }
83
+
84
+    @Data
85
+    @Accessors(chain = true)
86
+    public static class Captcha {
87
+        String key;
88
+        String value;
89
+        String image;
90
+        LocalDateTime expire;
91
+    }
92
+}

+ 3
- 3
src/main/java/com/huiju/estateagents/common/smsService/Captcha.java ファイルの表示

@@ -101,7 +101,7 @@ public class Captcha {
101 101
         // 过期则清除缓存
102 102
         boolean res = checkExpire(phone);
103 103
         if (res) {
104
-            clearCache(phone);
104
+            clearCache(tel);
105 105
         }
106 106
 
107 107
         return !res;
@@ -137,7 +137,7 @@ public class Captcha {
137 137
     public void clearUnused() {
138 138
         for (Phone phone: allPhones.values()) {
139 139
             if (checkExpire(phone)) {
140
-                clearCache(phone);
140
+                clearCache(phone.getNumber());
141 141
             }
142 142
         }
143 143
     }
@@ -148,7 +148,7 @@ public class Captcha {
148 148
     private void toCache(Phone phone) {
149 149
         allPhones.put(phone.getNumber(), phone);
150 150
     }
151
-    private void clearCache(Phone phone) { allPhones.remove(phone); }
151
+    private void clearCache(String key) { allPhones.remove(key); }
152 152
 
153 153
     private LocalDateTime expireTime() {
154 154
         return LocalDateTime.now().plusSeconds(expireSec);

+ 12
- 6
src/main/java/com/huiju/estateagents/controller/CommonController.java ファイルの表示

@@ -4,21 +4,18 @@ package com.huiju.estateagents.controller;
4 4
 import com.alibaba.fastjson.JSONObject;
5 5
 import com.huiju.estateagents.base.BaseController;
6 6
 import com.huiju.estateagents.base.ResponseBean;
7
-import com.huiju.estateagents.common.AliOSSUtils;
8
-import com.huiju.estateagents.common.CommConstant;
9
-import com.huiju.estateagents.common.SMSUtils;
10
-import com.huiju.estateagents.common.StringUtils;
7
+import com.huiju.estateagents.common.*;
11 8
 import com.huiju.estateagents.common.smsService.Captcha;
12 9
 import com.huiju.estateagents.service.IMiniAppService;
13 10
 import com.huiju.estateagents.service.ITdMiniappTemplateTypeService;
14 11
 import io.swagger.annotations.Api;
15 12
 import io.swagger.annotations.ApiOperation;
16
-import org.apache.ibatis.annotations.Mapper;
17 13
 import org.springframework.beans.factory.annotation.Autowired;
18 14
 import org.springframework.web.bind.annotation.*;
19 15
 import org.springframework.web.multipart.MultipartFile;
20 16
 
21 17
 import javax.servlet.http.HttpServletRequest;
18
+import javax.servlet.http.HttpServletResponse;
22 19
 import java.io.IOException;
23 20
 import java.util.ArrayList;
24 21
 import java.util.HashMap;
@@ -39,7 +36,10 @@ public class CommonController extends BaseController {
39 36
     IMiniAppService iMiniAppService;
40 37
 
41 38
     @Autowired
42
-    Captcha captchaServ;
39
+    Captcha captchaServ;    // 短信验证码
40
+
41
+    @Autowired
42
+    CaptchaUtils captchaUtils;  // 图片验证码
43 43
 
44 44
     /**
45 45
      * 图片
@@ -57,6 +57,12 @@ public class CommonController extends BaseController {
57 57
         }
58 58
     }
59 59
 
60
+    @ApiOperation("获取图片验证码")
61
+    @GetMapping("/admin/image-captcha")
62
+    public ResponseBean getCaptcha(HttpServletRequest request, HttpServletResponse response) {
63
+        return ResponseBean.success(captchaUtils.generate());
64
+    }
65
+
60 66
     @PostMapping("/admin/qrcode")
61 67
     public ResponseBean createQrCode(@RequestBody String jsonStr, HttpServletRequest request) {
62 68
         Integer orgId = getOrgId(request);

+ 1
- 0
src/main/java/com/huiju/estateagents/interceptor/AccessInterceptor.java ファイルの表示

@@ -67,6 +67,7 @@ public class AccessInterceptor implements HandlerInterceptor {
67 67
 		    "/api/center/signin",
68 68
             "/api/channel/signin",
69 69
 		    "/api/admin/taUser/signin",
70
+            "/api/admin/image-captcha",
70 71
             "/swagger-resources/configuration/ui",
71 72
             "/swagger-resources",
72 73
             "/v2/api-docs",