张延森 4 년 전
부모
커밋
ff8f3d9893

+ 7
- 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.1</version>
13
+	<version>1.0.4</version>
14 14
 	<name>xiangsong</name>
15 15
 	<description>香颂</description>
16 16
 
@@ -95,6 +95,12 @@
95 95
 			<artifactId>aliyun-sdk-oss</artifactId>
96 96
 			<version>2.8.3</version>
97 97
 		</dependency>
98
+
99
+		<dependency>
100
+			<groupId>com.aliyun</groupId>
101
+			<artifactId>aliyun-java-sdk-core</artifactId>
102
+			<version>[4.4.9,5.0.0)</version>
103
+		</dependency>
98 104
 		<!-- https://mvnrepository.com/artifact/org.dom4j/dom4j -->
99 105
 		<dependency>
100 106
 			<groupId>org.dom4j</groupId>

+ 157
- 0
src/main/java/com/huiju/estateagents/common/smsService/Captcha.java 파일 보기

@@ -0,0 +1,157 @@
1
+package com.huiju.estateagents.common.smsService;
2
+
3
+import com.alibaba.fastjson.JSONObject;
4
+import lombok.Data;
5
+import lombok.experimental.Accessors;
6
+import lombok.extern.slf4j.Slf4j;
7
+import org.springframework.beans.factory.annotation.Autowired;
8
+import org.springframework.beans.factory.annotation.Value;
9
+import org.springframework.scheduling.annotation.EnableScheduling;
10
+import org.springframework.scheduling.annotation.Scheduled;
11
+import org.springframework.stereotype.Component;
12
+
13
+import java.time.LocalDateTime;
14
+import java.util.HashMap;
15
+import java.util.Hashtable;
16
+import java.util.Map;
17
+import java.util.Random;
18
+
19
+@Slf4j
20
+@Component
21
+@EnableScheduling
22
+public class Captcha {
23
+    @Value("${sms-service.captcha.code}")
24
+    String code;
25
+
26
+    @Value("${sms-service.captcha.product}")
27
+    String product;
28
+
29
+    @Value("${sms-service.captcha.sign}")
30
+    String sign;
31
+
32
+    @Autowired
33
+    Service service;
34
+
35
+    // 默认超时时间, 单位秒
36
+    Integer expireSec = 5 * 60;
37
+
38
+    // 缓存电话列表
39
+    Hashtable<String, Phone> allPhones = new Hashtable<>();
40
+
41
+    /**
42
+     * 发送验证码
43
+     * @param tel
44
+     * @param captcha
45
+     * @throws Exception
46
+     */
47
+    public void send(String tel, String captcha) throws Exception {
48
+        if (null == captcha) {
49
+            throw new Exception("没有有效的验证码");
50
+        }
51
+
52
+        Map<String, String> content = new HashMap<>();
53
+        content.put("code", captcha);
54
+        content.put("product", product);
55
+
56
+        Phone phone = new Phone()
57
+                .setNumber(tel)
58
+                .setCaptcha(captcha)
59
+                .setExpire(expireTime());
60
+
61
+        Service.Message message = new Service.Message()
62
+                .setCode(code)
63
+                .setSign(sign)
64
+                .setTel(tel)
65
+                .setContent(JSONObject.toJSONString(content));
66
+
67
+        try {
68
+            service.sendMessage(message);
69
+        } catch (Exception e) {
70
+            // 修改友好点的错误
71
+            throw new Exception("发送验证码失败");
72
+        }
73
+
74
+        log.info(String.format("发送验证码 %s 到手机 %s", captcha, tel));
75
+
76
+        // 发送成功, 写入缓存
77
+        toCache(phone);
78
+    }
79
+
80
+    /**
81
+     * 校验验证码
82
+     * @param tel
83
+     * @param captcha
84
+     * @return
85
+     */
86
+    public boolean validate(String tel, String captcha) {
87
+        Phone phone = fromCache(tel);
88
+        if (null == phone) {
89
+            return false;
90
+        }
91
+
92
+        if (null == captcha || !captcha.equals(phone.getCaptcha())) {
93
+            return false;
94
+        }
95
+
96
+        // 过期则清除缓存
97
+        boolean res = checkExpire(phone);
98
+        if (res) {
99
+            clearCache(phone);
100
+        }
101
+
102
+        return !res;
103
+    }
104
+
105
+    /**
106
+     * 辅助方法: 随机生成验证码
107
+     * @param length
108
+     * @return
109
+     */
110
+    public String randCaptcha(int length) {
111
+        Random random = new Random();
112
+        String res = "";
113
+        for (int i = 0; i < length; i ++) {
114
+            int num = random.nextInt(10);
115
+            res += String.valueOf(num);
116
+        }
117
+        return res;
118
+    }
119
+
120
+    /**
121
+     * 定时任务 每半小时 清除一次不用的手机号
122
+     */
123
+    @Scheduled(fixedRate = 1000 * 60 * 30)
124
+    public void clearUnused() {
125
+        for (Phone phone: allPhones.values()) {
126
+            if (checkExpire(phone)) {
127
+                clearCache(phone);
128
+            }
129
+        }
130
+    }
131
+
132
+    private Phone fromCache(String phone) {
133
+        return allPhones.get(phone);
134
+    }
135
+    private void toCache(Phone phone) {
136
+        allPhones.put(phone.getNumber(), phone);
137
+    }
138
+    private void clearCache(Phone phone) { allPhones.remove(phone); }
139
+
140
+    private LocalDateTime expireTime() {
141
+        return LocalDateTime.now().plusSeconds(expireSec);
142
+    }
143
+
144
+    // 过期返回 true
145
+    private boolean checkExpire(Phone phone) {
146
+        return LocalDateTime.now().isAfter(phone.getExpire());
147
+    }
148
+
149
+    @Data
150
+    @Accessors(chain = true)
151
+    public static class Phone {
152
+        String number;
153
+        String captcha;
154
+        LocalDateTime expire;
155
+    }
156
+
157
+}

+ 72
- 0
src/main/java/com/huiju/estateagents/common/smsService/Service.java 파일 보기

@@ -0,0 +1,72 @@
1
+package com.huiju.estateagents.common.smsService;
2
+
3
+import com.aliyuncs.CommonRequest;
4
+import com.aliyuncs.CommonResponse;
5
+import com.aliyuncs.DefaultAcsClient;
6
+import com.aliyuncs.IAcsClient;
7
+import com.aliyuncs.http.MethodType;
8
+import com.aliyuncs.profile.DefaultProfile;
9
+import lombok.Data;
10
+import lombok.experimental.Accessors;
11
+import lombok.extern.slf4j.Slf4j;
12
+import org.springframework.beans.factory.InitializingBean;
13
+import org.springframework.beans.factory.annotation.Value;
14
+import org.springframework.stereotype.Component;
15
+
16
+@Slf4j
17
+@Component
18
+public class Service implements InitializingBean {
19
+
20
+    @Value("${sms-service.accessKeyId}")
21
+    String accessKeyId;
22
+
23
+    @Value("${sms-service.accessKeySecret}")
24
+    String accessKeySecret;
25
+
26
+    //产品名称:云通信短信API产品,开发者无需替换
27
+    static final String product = "Dysmsapi";
28
+    //产品域名,开发者无需替换
29
+    static final String domain = "dysmsapi.aliyuncs.com";
30
+    //API 的名称,开发者无需替换
31
+    static final String action = "SendSms";
32
+    //API RegionId,开发者无需替换
33
+    static final String regionId = "cn-hangzhou";
34
+    //API 版本,开发者无需替换
35
+    static final String verison = "2017-05-25";
36
+
37
+    IAcsClient acsClient;
38
+
39
+    public void sendMessage(Message message) throws Exception {
40
+        request(message);
41
+    }
42
+
43
+    private CommonResponse request(Message message) throws Exception {
44
+        CommonRequest request = new CommonRequest();
45
+        request.setSysMethod(MethodType.POST);
46
+        request.setSysDomain(domain);
47
+        request.setSysVersion(verison);
48
+        request.setSysAction(action);
49
+        request.putQueryParameter("RegionId", regionId);
50
+        request.putQueryParameter("PhoneNumbers", message.getTel());
51
+        request.putQueryParameter("SignName", message.getSign());
52
+        request.putQueryParameter("TemplateCode", message.getCode());
53
+        request.putQueryParameter("TemplateParam", message.getContent());
54
+
55
+        return acsClient.getCommonResponse(request);
56
+    }
57
+
58
+    @Override
59
+    public void afterPropertiesSet() throws Exception {
60
+        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
61
+        acsClient = new DefaultAcsClient(profile);
62
+    }
63
+
64
+    @Data
65
+    @Accessors(chain = true)
66
+    public static class Message {
67
+        String code;
68
+        String tel;
69
+        String sign;
70
+        String content;
71
+    }
72
+}

+ 9
- 5
src/main/java/com/huiju/estateagents/controller/CommonController.java 파일 보기

@@ -8,6 +8,7 @@ import com.huiju.estateagents.common.AliOSSUtils;
8 8
 import com.huiju.estateagents.common.CommConstant;
9 9
 import com.huiju.estateagents.common.SMSUtils;
10 10
 import com.huiju.estateagents.common.StringUtils;
11
+import com.huiju.estateagents.common.smsService.Captcha;
11 12
 import com.huiju.estateagents.service.IMiniAppService;
12 13
 import com.huiju.estateagents.service.ITdMiniappTemplateTypeService;
13 14
 import io.swagger.annotations.Api;
@@ -37,6 +38,9 @@ public class CommonController extends BaseController {
37 38
     @Autowired
38 39
     IMiniAppService iMiniAppService;
39 40
 
41
+    @Autowired
42
+    Captcha captchaServ;
43
+
40 44
     /**
41 45
      * 图片
42 46
      * @param multipartFile
@@ -119,10 +123,11 @@ public class CommonController extends BaseController {
119 123
     @ApiOperation(value = "发送验证码", notes = "发送验证码")
120 124
     @PostMapping("{plat}/captcha")
121 125
     public ResponseBean sendCaptcha(@RequestParam String tel) {
122
-        boolean success = smsUtils.sendCaptcha(tel);
123
-        if (success) {
126
+        try {
127
+            captchaServ.send(tel, captchaServ.randCaptcha(6));
124 128
             return ResponseBean.success("发送验证码成功");
125
-        } else {
129
+        } catch (Exception e) {
130
+            e.printStackTrace();
126 131
             return ResponseBean.error("验证码发送失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
127 132
         }
128 133
     }
@@ -130,8 +135,7 @@ public class CommonController extends BaseController {
130 135
     @ApiOperation(value = "校验验证码", notes = "校验验证码")
131 136
     @PostMapping("{plat}/check/captcha")
132 137
     public ResponseBean sendCaptcha(@RequestParam String tel,@RequestParam String captcha) {
133
-        boolean success = smsUtils.checkCaptcha(tel,captcha);
134
-        if (success) {
138
+        if (captchaServ.validate(tel, captcha)) {
135 139
             return ResponseBean.success("验证码校验成功");
136 140
         } else {
137 141
             return ResponseBean.error("验证码校验失败", ResponseBean.ERROR_UNAVAILABLE);

+ 13
- 1
src/main/java/com/huiju/estateagents/controller/TaPersonController.java 파일 보기

@@ -12,6 +12,7 @@ import com.huiju.estateagents.center.taUser.service.ITaUserService;
12 12
 import com.huiju.estateagents.common.CommConstant;
13 13
 import com.huiju.estateagents.common.SMSUtils;
14 14
 import com.huiju.estateagents.common.StringUtils;
15
+import com.huiju.estateagents.common.smsService.Captcha;
15 16
 import com.huiju.estateagents.entity.TaChannel;
16 17
 import com.huiju.estateagents.entity.TaChannelPerson;
17 18
 import com.huiju.estateagents.entity.TaPerson;
@@ -21,6 +22,7 @@ import com.huiju.estateagents.third.entity.TaThirdPartyMiniappConfig;
21 22
 import com.huiju.estateagents.third.service.ITaThirdPartyMiniappConfigService;
22 23
 import io.swagger.annotations.Api;
23 24
 import io.swagger.annotations.ApiOperation;
25
+import org.checkerframework.checker.units.qual.A;
24 26
 import org.springframework.beans.factory.annotation.Autowired;
25 27
 import org.springframework.web.bind.annotation.*;
26 28
 import org.springframework.web.client.RestTemplate;
@@ -54,6 +56,10 @@ public class TaPersonController extends BaseController {
54 56
     ITaUserService taUserService;
55 57
     @Autowired
56 58
     SMSUtils smsUtils;
59
+
60
+    @Autowired
61
+    Captcha captchaServ;
62
+
57 63
     @Autowired
58 64
     ITaPersonBuildingService taPersonBuildingService;
59 65
 
@@ -107,10 +113,16 @@ public class TaPersonController extends BaseController {
107 113
     @PutMapping("/wx/registerConsultant")
108 114
     public ResponseBean registerConsultant(HttpServletRequest request,@RequestParam String phone,@RequestParam String code) {
109 115
         ResponseBean responseBean = new ResponseBean();
110
-        if(!smsUtils.checkCaptcha(phone,code)){
116
+//        if(!smsUtils.checkCaptcha(phone,code)){
117
+//            responseBean.addError("验证码错误。");
118
+//            return responseBean;
119
+//        }
120
+
121
+        if (!captchaServ.validate(phone, code)) {
111 122
             responseBean.addError("验证码错误。");
112 123
             return responseBean;
113 124
         }
125
+
114 126
         Integer orgId =getOrgId(request);
115 127
         String openid = getOpenId(request);
116 128
         List<TaPerson> taPersons = taPersonService.getPersonsByOpenId(openid);

+ 3
- 2
src/main/resources/application-prod.yml 파일 보기

@@ -50,7 +50,8 @@ ali:
50 50
 # 手机短信服务
51 51
 sms:
52 52
   # 先使用外网地址, 后期使用内网地址
53
-  url: http://101.200.84.17:8081/sms
53
+  # http://101.200.84.17:8081/sms
54
+  url:
54 55
   method: POST
55 56
   contentType: "application/json;charset=UTF-8"
56 57
   template:
@@ -68,7 +69,7 @@ sms:
68 69
     # 验证码长度
69 70
     size: 4
70 71
     # 短信签名
71
-    sign: 橙蕉
72
+    sign: 远道
72 73
 
73 74
   visitor:
74 75
     code: "0501"

+ 7
- 1
src/main/resources/application.yml 파일 보기

@@ -3,4 +3,10 @@ spring:
3 3
   profiles:
4 4
     active: @profiles.active@
5 5
 
6
-
6
+sms-service:
7
+  accessKeyId: LTAIGQtIi28MfXms
8
+  accessKeySecret: b6maakcdzEywmkhcK15rStx54hOiIA
9
+  captcha:
10
+    code: SMS_27440200
11
+    product: 【远道荟】
12
+    sign: 身份验证