Переглянути джерело

API 发送短信配置优化

limingming 1 рік тому
батько
коміт
c3116974d1

+ 0 - 41
rouyi-api/src/main/java/com/info666/infraredRemote/config/SMSProperties.java

@@ -1,41 +0,0 @@
-package com.info666.infraredRemote.config;
-
-import lombok.Data;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.stereotype.Component;
-
-/**
- * @author liming
- * @date 2023/08/02
- */
-@Data
-@Component
-public class SMSProperties {
-
-    /**
-     * accessKeyId
-     */
-    @Value("${aliyun.sms.accessKeyId}")
-    private String accessKeyId;
-
-    /**
-     * accessKeySecret
-     */
-    @Value("${aliyun.sms.accessKeySecret}")
-    private String accessKeySecret;
-
-    /**
-     * 短信签名
-     */
-    @Value("${aliyun.sms.signName}")
-    private String signName;
-
-    /**
-     * 短信模板
-     */
-    @Value("${aliyun.sms.templateCode}")
-    private String templateCode;
-
-
-
-}

+ 6 - 3
rouyi-api/src/main/java/com/info666/infraredRemote/controller/SmsController.java

@@ -2,11 +2,11 @@ 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.RedisKeys;
 import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.ip.IpUtils;
 import com.ruoyi.system.domain.UserInfo;
 import com.ruoyi.system.domain.vo.SmsResponseVo;
 import org.apache.commons.lang3.RandomStringUtils;
@@ -33,6 +33,9 @@ public class SmsController {
     @Autowired
     private RedisTemplate redisTemplate;
 
+    @Autowired
+    private SendMessage sendMessage;
+
 
     @PostMapping("/send")
     public R<SmsResponseVo> sendSms(@RequestBody UserInfo param, HttpServletRequest request) {
@@ -42,7 +45,7 @@ public class SmsController {
 
         //每个公网id地址1天只能发100条
         //每个手机号一天只能发10条,一分钟只能发一条
-        String ip = IPUtils.getIpAddr(request);
+        String ip = IpUtils.getIpAddr(request);
         DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");
         String strDate = dtf.format(LocalDate.now());
         String ipKey = RedisKeys.INCR_IP + strDate + "_" + ip;
@@ -86,7 +89,7 @@ public class SmsController {
         SmsResponseVo smsResponse = new SmsResponseVo();
         try {
             //手机号和验证码进行发短信
-            SendSmsResponse sendSmsResponse = SendMessage.sendSms(param.getUserPhone(), code);
+            SendSmsResponse sendSmsResponse = sendMessage.sendSms(param.getUserPhone(), code);
 
             if (sendSmsResponse.getMessage().equals("OK")) {
                 redisCache.setCacheObject(RedisKeys.SMS_PHONE_CODE + param.getUserPhone(), code, 5, TimeUnit.MINUTES);

+ 5 - 4
rouyi-api/src/main/resources/application.yml

@@ -152,10 +152,11 @@ xss:
 #自定义短信模板配置
 aliyun:
   sms:
-    accessKeyId: LTAI5tKvxXUQ61e5PSm3mzQ1
-    accessKeySecret: WEzhPLHWqEINBbiLn168g2nP1H8Cje
+    accessKeyId: LTAI4GEnYjNkDo3PiCLwmcsS
+    accessKeySecret: PVJvmoubjj3BTRViAxPpcFZvEyAeOj
     #短信签名
-    signName: 阿里云短信测试
+    signName: 菊辉电子
     #短信模板
-    templateCode: SMS_154950909
+    templateCode: SMS_283430418
+
 

+ 43 - 0
ruoyi-common/src/main/java/com/ruoyi/common/config/SMSProperties.java

@@ -0,0 +1,43 @@
+package com.ruoyi.common.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author liming
+ * @date 2023/08/02
+ */
+@Data
+@Configuration
+@ConfigurationProperties(prefix = "aliyun.sms")
+public class SMSProperties {
+
+    /**
+     * accessKeyId
+     */
+//    @Value("${aliyun.sms.accessKeyId}")
+    private String accessKeyId;
+
+
+    /**
+     * accessKeySecret
+     */
+//    @Value("${aliyun.sms.accessKeySecret}")
+    private String accessKeySecret;
+
+    /**
+     * 短信签名
+     */
+//    @Value("${aliyun.sms.signName}")
+    private String signName;
+
+    /**
+     * 短信模板
+     */
+//    @Value("${aliyun.sms.templateCode}")
+    private String templateCode;
+
+
+
+}

+ 0 - 10
ruoyi-common/src/main/java/com/ruoyi/common/core/redis/RedisCache.java

@@ -263,14 +263,4 @@ 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);
-    }
-
 }

+ 11 - 29
ruoyi-common/src/main/java/com/ruoyi/common/core/sms/SendMessage.java

@@ -7,6 +7,9 @@ import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
 import com.aliyuncs.exceptions.ClientException;
 import com.aliyuncs.profile.DefaultProfile;
 import com.aliyuncs.profile.IClientProfile;
+import com.ruoyi.common.config.SMSProperties;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
 
 /**
  * Created on 20/5/19.
@@ -16,28 +19,20 @@ import com.aliyuncs.profile.IClientProfile;
  * 备注:Demo工程编码采用UTF-8
  * 国际短信发送请勿参照此DEMO
  */
+@Component
 public class SendMessage {
-    //产品名称:云通信短信API产品,开发者无需替换
-    static final String product = "Dysmsapi";
-    //产品域名,开发者无需替换
-    static final String domain = "dysmsapi.aliyuncs.com";
 
-    // TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
-//    static final String accessKeyId = "LTAI5tKvxXUQ61e5PSm3mzQ1";
-    static final String accessKeyId = "LTAI4GEnYjNkDo3PiCLwmcsS";
-
-//    static final String accessKeySecret = "WEzhPLHWqEINBbiLn168g2nP1H8Cje";
-    static final String accessKeySecret = "PVJvmoubjj3BTRViAxPpcFZvEyAeOj";
-
-    public static SendSmsResponse sendSms(String phone, String code) throws ClientException {
+    @Autowired
+    private  SMSProperties smsProperties;
 
+    public  SendSmsResponse sendSms(String phone, String code) throws ClientException {
         //可自助调整超时时间
         System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
         System.setProperty("sun.net.client.defaultReadTimeout", "10000");
 
         //初始化acsClient,暂不支持region化
-        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
-        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
+        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", smsProperties.getAccessKeyId(), smsProperties.getAccessKeySecret());
+        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
         IAcsClient acsClient = new DefaultAcsClient(profile);
 
         //组装请求对象-具体描述见控制台-文档部分内容
@@ -45,10 +40,9 @@ public class SendMessage {
         //必填:待发送手机号
         request.setPhoneNumbers(phone);
         //必填:短信签名-可在短信控制台中找到
-        request.setSignName("菊辉电子");
+        request.setSignName(smsProperties.getSignName());
         //必填:短信模板-可在短信控制台中找到
-//        request.setTemplateCode("SMS_154950909");
-        request.setTemplateCode("SMS_283430418");
+        request.setTemplateCode(smsProperties.getTemplateCode());
         //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
         request.setTemplateParam("{\"code\":\"" + code + "\"}");
 
@@ -64,16 +58,4 @@ public class SendMessage {
         return sendSmsResponse;
     }
 
-
-    public static void main(String[] args) throws ClientException, InterruptedException {
-
-        //发短信
-        SendSmsResponse response = sendSms("123456789", "6666");
-        System.out.println("短信接口返回的数据----------------");
-        System.out.println("Code=" + response.getCode());
-        System.out.println("Message=" + response.getMessage());
-        System.out.println("RequestId=" + response.getRequestId());
-        System.out.println("BizId=" + response.getBizId());
-        
-    }
 }