042 用户注册功能02--阿里云短信验证功能
1.阿里云短信服务申请
(1)访问阿里云主页:https://www.aliyun.com/?utm_content=se_1002986586
(2)登录:
(3)进入云通信:
(4)进入短信服务控制台:
(5)点击管理控制台后,进入如下页面
(6)进入如下页面:https://help.aliyun.com/product/44282.html?spm=5176.12207334.0.0.81701cbecbbDEP
(7)点击完整接入流程,进入如下页面
短信使用流程见下图:
<1>入驻阿里云
<2>获取AccessKey
点击创建AccessKey后,进入新的帮助页面
<3>创建签名和模版
<4>短信接口配置
package utils; import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonResponse; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; /* pom.xml <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.0.3</version> </dependency> */ public class SmsUtil { public static void main(String[] args) { //注意:"<accessKeyId>", "<accessSecret>"应填真实的accessKeyId和accessSecret DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>"); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setAction("SendSms"); request.putQueryParameter("RegionId", "cn-hangzhou"); request.putQueryParameter("PhoneNumbers", "17826828544"); request.putQueryParameter("SignName", "aozhanlucky"); request.putQueryParameter("TemplateCode", "SMS_174806157"); request.putQueryParameter("TemplateParam", "{'code':123456}"); try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } }
pom依赖:
<dependencies> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.0.3</version> </dependency> </dependencies>
2.短信微服务
另外,因为短信发送API调用时长的不确定性,为了提高程序的响应速度,短信发送我们都将采用异步发送方式,即:
-
短信服务监听MQ消息,收到消息后发送短信。
-
其它服务要发送短信时,通过MQ通知短信微服务。
(1)
(2)pom文件
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>leyou</artifactId> <groupId>lucky.leyou.parent</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <groupId>lucky.leyou.sms</groupId> <artifactId>leyou-sms-service</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.0.3</version> </dependency> </dependencies> </project>
(3)
package lucky.leyou; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * 微服务启动器类,即微服务的入口类 */ @SpringBootApplication public class LeyouSmsApplication { public static void main(String[] args) { SpringApplication.run(LeyouSmsApplication.class, args); } }
(4)
server: port: 8089 spring: application: name: sms-service rabbitmq: host: 127.0.0.1 username: guest password: guest virtual-host: /
3.
leyou:
sms:
accessKeyId: JWffwFJIwada # 你自己的accessKeyId
accessKeySecret: aySRliswq8fe7rF9gQyy1Izz4MQ # 你自己的AccessKeySecret
signName: 乐优商城 # 签名名称
verifyCodeTemplate: SMS_133976814 # 模板名称
然后注入到属性类中:
package lucky.leyou.sms.config; import org.springframework.boot.context.properties.ConfigurationProperties; /** * application.yml文件中所存储的数据对应实体类 */ @ConfigurationProperties(prefix = "leyou.sms") public class SmsProperties { String accessKeyId; String accessKeySecret; String signName; String verifyCodeTemplate; public String getAccessKeyId() { return accessKeyId; } public void setAccessKeyId(String accessKeyId) { this.accessKeyId = accessKeyId; } public String getAccessKeySecret() { return accessKeySecret; } public void setAccessKeySecret(String accessKeySecret) { this.accessKeySecret = accessKeySecret; } public String getSignName() { return signName; } public void setSignName(String signName) { this.signName = signName; } public String getVerifyCodeTemplate() { return verifyCodeTemplate; } public void setVerifyCodeTemplate(String verifyCodeTemplate) { this.verifyCodeTemplate = verifyCodeTemplate; } }
(2)
package lucky.leyou.sms.utils; import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonResponse; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.http.MethodType; import com.aliyuncs.profile.DefaultProfile; import lucky.leyou.sms.config.SmsProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.stereotype.Component; /* pom.xml <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.0.3</version> </dependency> */ @Component @EnableConfigurationProperties(SmsProperties.class) public class SmsUtil { @Autowired private SmsProperties smsProperties; public static void main(String[] args) { //注意:"<accessKeyId>", "<accessSecret>"应填真实的accessKeyId和accessSecret //DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>"); DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FpdmrvsFqDVBR1k554E", "816adyOkUF2b4d4ZyjbJnSG2rOoxf7"); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setAction("SendSms"); request.putQueryParameter("RegionId", "cn-hangzhou"); request.putQueryParameter("PhoneNumbers", "17826828544"); request.putQueryParameter("SignName", "aozhanlucky"); request.putQueryParameter("TemplateCode", "SMS_174806157"); request.putQueryParameter("TemplateParam", "{'code':824520}"); try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } public void sendSms(String phone, String code, String signName, String verifyCodeTemplate) { //注意:"<accessKeyId>", "<accessSecret>"应填真实的accessKeyId和accessSecret DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>"); IAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setAction("SendSms"); request.putQueryParameter("RegionId", "cn-hangzhou"); request.putQueryParameter("PhoneNumbers", phone); request.putQueryParameter("SignName", signName); request.putQueryParameter("TemplateCode", verifyCodeTemplate); request.putQueryParameter("TemplateParam", "{'code':"+code+"}"); try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } }
(3)
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency>
-
phone:电话号码
-
package lucky.leyou.sms.listener; import lucky.leyou.sms.config.SmsProperties; import lucky.leyou.sms.utils.SmsUtil; import org.apache.commons.lang3.StringUtils; import org.springframework.amqp.rabbit.annotation.Exchange; import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.QueueBinding; import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Map; @Component @EnableConfigurationProperties(SmsProperties.class) public class SmsListener { @Autowired private SmsUtil smsUtils; @Autowired private SmsProperties prop; @RabbitListener(bindings = @QueueBinding( value = @Queue(value = "leyou.sms.queue", durable = "true"), exchange = @Exchange(value = "leyou.sms.exchange", ignoreDeclarationExceptions = "true"), key = {"sms.verify.code"})) public void listenSms(Map<String, String> msg) throws Exception { if (msg == null || msg.size() <= 0) { // 放弃处理 return; } String phone = msg.get("phone"); String code = msg.get("code"); if (StringUtils.isBlank(phone) || StringUtils.isBlank(code)) { // 放弃处理 return; } // 发送消息 this.smsUtils.sendSms(phone, code, prop.getSignName(), prop.getVerifyCodeTemplate()); } }
(4)启动项目,然后查看RabbitMQ控制台,发现交换机已经创建:
队列也已经创建:
并且绑定:
分类:
乐优商城项目
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)