Forráskód Böngészése

Api短信验证码逻辑偶优化

limingming 1 éve
szülő
commit
84e6cc3744

+ 62 - 4
rouyi-api/src/main/java/com/info666/infraredRemote/controller/SmsController.java

@@ -2,19 +2,26 @@ package com.info666.infraredRemote.controller;
 
 import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
 import com.ruoyi.common.core.domain.R;
+import com.ruoyi.common.core.ip.IPUtils;
 import com.ruoyi.common.core.redis.RedisCache;
 import com.ruoyi.common.core.sms.SendMessage;
+import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.system.domain.UserInfo;
 import com.ruoyi.system.domain.vo.SmsResponseVo;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
+import javax.servlet.http.HttpServletRequest;
+import java.time.LocalDate;
+import java.time.format.DateTimeFormatter;
 import java.util.UUID;
 import java.util.concurrent.TimeUnit;
+import java.util.regex.Pattern;
 
 @RestController
 @RequestMapping("/api/v1/sms")
@@ -22,9 +29,57 @@ public class SmsController {
 
     @Autowired
     private RedisCache redisCache;
+    @Autowired
+    private RedisTemplate redisTemplate;
+
 
     @PostMapping("/send")
-    public R<SmsResponseVo> sendSms(@RequestBody UserInfo param) {
+    public R<SmsResponseVo> sendSms(@RequestBody UserInfo param, HttpServletRequest request) {
+        if (StringUtils.isEmpty(param.getUserPhone()) || Pattern.matches("^1[3-9]\\d{9}$\n",param.getUserPhone())){
+            return R.fail("手机号格式不正确");
+        }
+
+        //每个公网id地址1天只能发100条
+        //每个手机号一天只能发10条,一分钟只能发一条
+        String ip = IPUtils.getIpAddr(request);
+        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
+        String strDate = dtf.format(LocalDate.now());
+        String ipKey = strDate + "_" + ip;
+        String phoneIconKey = strDate + "_"+"icon" + param.getUserPhone();
+        String phone = "phone_"+param.getUserPhone();
+        //一分钟发送一次
+        if (redisTemplate.hasKey(phone)) {
+            //存在
+            return R.fail("1分钟内只能发送一次,请稍后再试!");
+        } else {
+            //不存在
+            redisTemplate.opsForValue().set(phone, 1, 1L, TimeUnit.MINUTES);
+        }
+
+        //手机号
+        if (redisTemplate.hasKey(phoneIconKey)) {
+            //存在
+            Long increment = redisTemplate.opsForValue().increment(phoneIconKey, 1);
+            if (increment > 10) {
+                return R.fail("该手机号发送次数过多,已禁止发送!");
+            }
+        } else {
+            //不存在
+            redisTemplate.opsForValue().set(phoneIconKey, 1, 1L, TimeUnit.DAYS);
+        }
+
+        //ip
+        if (redisTemplate.hasKey(ipKey)) {
+            //存在
+            Long increment = redisTemplate.opsForValue().increment(ipKey, 1);
+            if (increment > 100) {
+                return R.fail("发送次数过多,已禁止发送!");
+            }
+        } else {
+            //不存在
+            redisTemplate.opsForValue().set(ipKey, 1, 1L, TimeUnit.DAYS);
+        }
+
         // 通过RandomStringUtils生成随机验证码
         String code = RandomStringUtils.randomNumeric(6);
         SmsResponseVo smsResponse = new SmsResponseVo();
@@ -32,12 +87,15 @@ public class SmsController {
             //手机号和验证码进行发短信
             SendSmsResponse sendSmsResponse = SendMessage.sendSms(param.getUserPhone(), code);
 
-            if (sendSmsResponse.getMessage().equals("OK")){
-                String uuid = UUID.randomUUID().toString().replaceAll("-","");
-                redisCache.setCacheObject(uuid,code,5, TimeUnit.MINUTES);
+            if (sendSmsResponse.getMessage().equals("OK")) {
+                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
+                redisCache.setCacheObject(uuid, code, 5, TimeUnit.MINUTES);
                 smsResponse.setCode(sendSmsResponse.getCode());
                 smsResponse.setMessage(sendSmsResponse.getMessage());
                 smsResponse.setKeyCode(uuid);
+
+            }else {
+                return R.fail(sendSmsResponse.getMessage());
             }
         } catch (Exception e) {
             e.printStackTrace();

+ 3 - 2
ruoyi-admin/src/main/resources/application.yml

@@ -3,13 +3,14 @@ ruoyi:
   # 名称
   name: RuoYi
   # 版本
-  version: 3.8.6
+  version: 1.0.0
   # 版权年份
   copyrightYear: 2023
   # 实例演示开关
   demoEnabled: true
   # 文件路径 示例( Windows配置D:/ruoyi/uploadPath,Linux配置 /home/ruoyi/uploadPath)
-  profile: /home/ruoyi/uploadPath
+#  profile: /home/ruoyi/uploadPath
+  profile: C:/Users/L.M/Desktop/ruoyi/uploadPath
   # 获取ip地址开关
   addressEnabled: false
   # 验证码类型 math 数字计算 char 字符验证

+ 14 - 6
ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java

@@ -1,11 +1,5 @@
 package com.ruoyi.common.core.redis;
 
-import java.util.Collection;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.TimeUnit;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.BoundSetOperations;
 import org.springframework.data.redis.core.HashOperations;
@@ -13,6 +7,9 @@ import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.data.redis.core.ValueOperations;
 import org.springframework.stereotype.Component;
 
+import java.util.*;
+import java.util.concurrent.TimeUnit;
+
 /**
  * spring redis 工具类
  *
@@ -265,4 +262,15 @@ public class RedisCache
     {
         return redisTemplate.keys(pattern);
     }
+
+
+
+    public static int incrementInt(final String key) {
+        return incrementInt(key,1);
+    }
+
+    public static int incrementInt(final String key,final int delta) {
+        return incrementInt(key,1);
+    }
+
 }