From fd44a03d00f930af97f0a115e06640d8b0b9529e Mon Sep 17 00:00:00 2001 From: haotian <2421912570@qq.com> Date: Mon, 12 May 2025 14:51:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=99=BE=E5=BA=A6=E7=9F=AD?= =?UTF-8?q?=E4=BF=A1=E9=AA=8C=E8=AF=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-admin/pom.xml | 362 ++++++++++-------- .../web/controller/app/baidu/BaiduSMS.java | 42 ++ .../web/controller/app/baidu/TestBaidu.java | 1 + .../web/controller/app/baidu/TestSmsv3.java | 14 + .../controller/common/CaptchaController.java | 16 +- 5 files changed, 266 insertions(+), 169 deletions(-) create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/baidu/BaiduSMS.java create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/baidu/TestSmsv3.java diff --git a/ruoyi-admin/pom.xml b/ruoyi-admin/pom.xml index a5a50621..f2b9be02 100644 --- a/ruoyi-admin/pom.xml +++ b/ruoyi-admin/pom.xml @@ -1,169 +1,195 @@ - - - - ruoyi - com.ruoyi - 3.8.5 - - 4.0.0 - jar - ruoyi-admin - - - web服务入口 - - - - - - - - org.springframework.boot - spring-boot-devtools - true - - - - - io.springfox - springfox-boot-starter - - - - - io.swagger - swagger-models - 1.6.2 - - - - - mysql - mysql-connector-java - - - - - - com.ruoyi - ruoyi-framework - - - - - com.ruoyi - ruoyi-quartz - - - - - com.ruoyi - ruoyi-generator - - - - - com.baidu.aip - java-sdk - 4.16.10 - - - - org.slf4j - slf4j-api - - - org.slf4j - slf4j-simple - - - - - - - org.netbeans.external - com-google-gson - RELEASE113 - - - - com.squareup.okhttp3 - okhttp - 4.9.3 - - - - - - com.jeequan - jeepay-sdk-java - 1.5.0 - - - - - com.alibaba - fastjson - 1.2.78 - - - - org.apache.httpcomponents - httpclient - 4.2.1 - - - org.apache.httpcomponents - httpcore - 4.2.1 - - - commons-lang - commons-lang - 2.6 - - - - junit - junit - 4.5 - test - - - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - 2.1.1.RELEASE - - true - - - - - repackage - - - - - - org.apache.maven.plugins - maven-war-plugin - 3.1.0 - - false - ${project.artifactId} - - - - ${project.artifactId} - - + + + + ruoyi + com.ruoyi + 3.8.5 + + 4.0.0 + jar + ruoyi-admin + + + web服务入口 + + + + + + + + org.springframework.boot + spring-boot-devtools + true + + + + + io.springfox + springfox-boot-starter + + + + + io.swagger + swagger-models + 1.6.2 + + + + + mysql + mysql-connector-java + + + + + + com.ruoyi + ruoyi-framework + + + + + com.ruoyi + ruoyi-quartz + + + + + com.ruoyi + ruoyi-generator + + + + + com.baidu.aip + java-sdk + 4.16.10 + + + + org.slf4j + slf4j-api + + + org.slf4j + slf4j-simple + + + + + + + + com.baidubce + bce-java-sdk + 0.10.282 + + + + org.slf4j + slf4j-reload4j + + + + + + + + + + + + + + + + + + org.netbeans.external + com-google-gson + RELEASE113 + + + + com.squareup.okhttp3 + okhttp + 4.9.3 + + + + + + com.jeequan + jeepay-sdk-java + 1.5.0 + + + + + com.alibaba + fastjson + 1.2.78 + + + + + + + + + + + + + + commons-lang + commons-lang + 2.6 + + + + junit + junit + 4.5 + test + + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + 2.1.1.RELEASE + + true + + + + + repackage + + + + + + org.apache.maven.plugins + maven-war-plugin + 3.1.0 + + false + ${project.artifactId} + + + + ${project.artifactId} + + \ No newline at end of file diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/baidu/BaiduSMS.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/baidu/BaiduSMS.java new file mode 100644 index 00000000..8298babb --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/baidu/BaiduSMS.java @@ -0,0 +1,42 @@ +package com.ruoyi.web.controller.app.baidu; + +import com.baidubce.auth.DefaultBceCredentials; +import com.baidubce.services.sms.SmsClient; +import com.baidubce.services.sms.SmsClientConfiguration; +import com.baidubce.services.sms.model.SendMessageV3Request; +import com.baidubce.services.sms.model.SendMessageV3Response; + +import java.util.HashMap; +import java.util.Map; + +public class BaiduSMS { + + public static boolean sendSMS(String phone, String code, String time) { + String ACCESS_KEY_ID = "84bd8ab3c233461dba7f254fd3b13e12"; + String SECRET_ACCESS_KEY = "ea723b36154c42c0bac3f3864929a724"; + String ENDPOINT = "http://smsv3.bj.baidubce.com"; + + SmsClientConfiguration config = new SmsClientConfiguration(); + config.setCredentials(new DefaultBceCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY)); + config.setEndpoint(ENDPOINT); + SmsClient client = new SmsClient(config); + + SendMessageV3Request request = new SendMessageV3Request(); + request.setMobile(phone); + request.setSignatureId("sms-sign-MiTUWW85061"); + request.setTemplate("sms-tmpl-iLlROF73209"); + Map contentVar = new HashMap<>(); + contentVar.put("SMSvCode", code); + contentVar.put("time", time); + request.setContentVar(contentVar); + SendMessageV3Response response = client.sendMessage(request); + // 解析请求响应 response.isSuccess()为true 表示成功 + if (response != null && response.isSuccess()) { + return true; + // submit success + } else { + // fail + return false; + } + } +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/baidu/TestBaidu.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/baidu/TestBaidu.java index 060c947a..2232b18e 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/baidu/TestBaidu.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/baidu/TestBaidu.java @@ -15,5 +15,6 @@ public class TestBaidu { System.out.println(calculationTime); + } } diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/baidu/TestSmsv3.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/baidu/TestSmsv3.java new file mode 100644 index 00000000..2daf9faf --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/app/baidu/TestSmsv3.java @@ -0,0 +1,14 @@ +package com.ruoyi.web.controller.app.baidu; + + +public class TestSmsv3 { + public static void main(String... args) { + boolean flag = BaiduSMS.sendSMS("18321826195", "1234", "5"); + if(flag){ + System.out.println("发送成功"); + } + else{ + System.out.println("发送失败"); + } + } +} diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java index 9e9bfa64..d64575c6 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CaptchaController.java @@ -2,6 +2,7 @@ package com.ruoyi.web.controller.common; import java.awt.image.BufferedImage; import java.io.IOException; +import java.util.Random; import java.util.concurrent.TimeUnit; import javax.annotation.Resource; import javax.imageio.ImageIO; @@ -10,6 +11,7 @@ import javax.servlet.http.HttpServletResponse; import com.ruoyi.common.core.domain.model.AppRegisterUser; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.service.ISysUserService; +import com.ruoyi.web.controller.app.baidu.BaiduSMS; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -192,9 +194,21 @@ public class CaptchaController { try { String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + phone; // 生成短信验证码 - String testCode = "1234"; +// String testCode = "1234"; + Random random = new Random(); + String testCode = String.format("%04d", random.nextInt(10000)); // 0000-9999 // Constants.CAPTCHA_EXPIRATION 为验证码过期时间,这里是5 redisCache.setCacheObject(verifyKey, testCode, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); + + // 发送验证码 + boolean flag = BaiduSMS.sendSMS(phone, testCode, "5"); + if (flag) { + log.info("发送短信验证码成功:"+phone); + System.out.println("发送短信验证码成功"); + } else { + log.info("发送短信验证码异常:"+phone); + System.out.println("发送短信验证码失败"); + } } catch (Exception e) { log.info("发送短信验证码异常:" + e.getMessage()); e.printStackTrace();