当前位置:网站首页>19:第三章:开发通行证服务:2:在程序中,打通阿里云短信服务;(仅仅是打通阿里云短信服务器,不涉及具体的业务开发)
19:第三章:开发通行证服务:2:在程序中,打通阿里云短信服务;(仅仅是打通阿里云短信服务器,不涉及具体的业务开发)
2022-06-26 10:24:00 【小枯林】
说明:
(1)本篇博客需要注意的点:
● 本篇博客仅仅是打通阿里云短信服务,不涉及【阿里云短信服务,在项目中的具体应用】,也不涉及【具体业务的开发】;
● 阿里云短信服务,作为一个第三方的“工具”,我们把其写在了【imooc-news-dev-common】通用工程中;
目录
二:在【imooc-news-dev-common】通用工程中,获取阿里云的秘钥;
1.在【imooc-news-dev-common】通用工程中,创建资源文件:aliyun.properties:保存阿里云短信服务的秘钥;
2.在【imooc-news-dev-common】通用工程中,创建“获取资源文件信息”的配置类:AliyunResource类:从aliyun.properties中获取秘钥;
4.在【imooc-news-dev-common】通用工程中,引入spring相关依赖、阿里云相关依赖;
1.在【imooc-news-dev-common】通用工程中,创建SMSUtils工具类;(这个工具类的作用,就是发送短信的)
2.在业务代码中,调用【发送短信的SMSUtils工具类】;
(1)首先,在【imooc-news-dev-api】接口工程中,定义一个接口;
(2)然后,在【imooc-news-dev-user】这个用户微服务中,去实现接口;
一:购买阿里云短信云服务;
在我们程序中,要想使用这个阿里云短信服务,是需要这个秘钥的;
二:在【imooc-news-dev-common】通用工程中,获取阿里云的秘钥;
1.在【imooc-news-dev-common】通用工程中,创建资源文件:aliyun.properties:保存阿里云短信服务的秘钥;
PS:这个配置文件名字,可以随便起;
2.在【imooc-news-dev-common】通用工程中,创建“获取资源文件信息”的配置类:AliyunResource类:从aliyun.properties中获取秘钥;
创建com.imooc.utils.extend包,并创建AliyunResource类;
AliyunResource类:
package com.imooc.utils.extend; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component //因为,我们类对象,也希望使用IoC管理起来; @PropertySource("classpath:aliyun.properties") //配置我么配置文件的路径; @ConfigurationProperties(prefix = "aliyun") // public class AliyunResource { private String accessKeyId; private String accessKeySecret; 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; } }说明:
(1)因为,这儿需要使用Spring的内容,所以我们需要在【common】中引入spring相关依赖,这在第四部分中,有介绍;
(2)配置类内容说明;通过这个配置类,我们就可以获取aliyun.properties中的阿里云秘钥了;
4.在【imooc-news-dev-common】通用工程中,引入spring相关依赖、阿里云相关依赖;
<!-- 引入SpringBoot 依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency> <!-- 第三方云厂商相关的依赖,阿里云 --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.0</version> </dependency> </dependencies>说明:
(1)其实,对于这个项目来说,一个说了N遍的东西:【各个微服务】依赖【api】、【api】依赖【model】、【model】依赖【common】;
● 我们在【11:第二章:架构后端项目:7:api接口暴露;(使用【api接口工程】管理【微服务的接口】)】 中,就在【api】中引入了spring相关依赖;;;当时的想法是,只要我们在【api】中引入了spring相关起来,那么在后面依赖【api】的各个【微服务】中,就不用重复引入了;
● 但是,现在我们的【common】也需要使用spring相关依赖了;;;所以,因为有上面的依赖关系,我们干脆把spring相关依赖给提到【common】中,然后把【api】中引入的spring依赖给删了,也是OK的;
(2)一般我们在顶级父工程中,对常用的依赖已经进行了设置,对其版本已经进行了设置;
● 但是,我们的【common】通用工程,其作用就是包含一些基础的组件、util工具类、枚举类等等基础的东西;;;所以,对于一些第三方的依赖,我们可以把其写在【common】中,同时在【common】去写版本;
● 自然,对于有些第三方依赖,我们也想把其定义在顶级父工程中,也是可以的;
三:在自己的程序中,打通阿里云短信服务;
1.在【imooc-news-dev-common】通用工程中,创建SMSUtils工具类;(这个工具类的作用,就是发送短信的)
package com.imooc.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 com.imooc.utils.extend.AliyunResource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class SMSUtils { @Autowired public AliyunResource aliyunResource; /** * 发送短信的方法 * @param mobile:手机号 * @param code:验证码; */ public void sendSMS(String mobile, String code) { //首先,构建一个profile,即是一种认证; DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", aliyunResource.getAccessKeyId(),//获取秘钥id aliyunResource.getAccessKeySecret());//获取秘钥 //创建一个client,其可以用来发起一个调用请求; IAcsClient client = new DefaultAcsClient(profile); //然后,构建一个request请求; CommonRequest request = new CommonRequest(); request.setSysMethod(MethodType.POST);//调用请求,采用的是POST形式; request.setSysDomain("dysmsapi.aliyuncs.com");//这儿的domain是个固定的 request.setSysVersion("2017-05-25");//版本号 request.setSysAction("SendSms");//发送的动作,直接固定使用这个即可; request.putQueryParameter("RegionId", "cn-hangzhou");//设置区域的id,还是杭州的这个 //具体,还要设置短信发送的一些设置; request.putQueryParameter("PhoneNumbers", mobile);//设置发送的手机号 request.putQueryParameter("SignName", "阿里云短信测试");//短信签名,因为自己的项目目前并没有备案,所以使用的是其测试的 request.putQueryParameter("TemplateCode", "SMS_154950909");//发送短信的模板号;(即发送短信,需要遵守的模板) request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");//具体的验证码,这儿code需要是一个JSON对象; try { //上面,构架好请求后;;就可以直接使用client去发送请求; CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } }说明:
(1)在阿里云短信服务处,也给提供了一个demo,可以去参考;
(2)内容说明;
(3)内容说明;
(4)内容说明;
而且,可以看到其code是要求是一个JSON的,所以我么在代码中才会写成【"{\"code\":\"" + code + "\"}"】;(本应该是【"{"code": "code" }】,只是这儿是引号嵌套,使用了\转义符而已)
(5)此时,这个发送短信的SMSUtils工具类,就差不多OK了;;;后面,在其他地方,就可以调用了;
2.在业务代码中,调用【发送短信的SMSUtils工具类】;
(1)首先,在【imooc-news-dev-api】接口工程中,定义一个接口;
说明:
(1)对于这中设计思想不明白,可以参考【11:第二章:架构后端项目:7:api接口暴露;(使用【api接口工程】管理【微服务的接口】)】;
……………………………………………………
(2)然后,在【imooc-news-dev-user】这个用户微服务中,去实现接口;
3.实际测试;
(1)首先,整个项目全局install一下;
(2)然后,启动【user】的主启动类;
(3)然后,访问【user】的【"/getSMSCode"】接口;
四:Summary;
(1)至此,阿里云短信服务,就算是打通了;;;后面,我们在开发具体业务的时候,就可以具体使用了;;;后面,我们也会使用redis来存放验证码等信息;
(2)阿里云短信服务的风控功能;
边栏推荐
- 2020.7.6 interview with fence network technology company
- Docker中实现MySQL主从复制
- 深度理解STM32的串口實驗(寄存器)【保姆級教程】
- That is to say, "live broadcast" is launched! One stop live broadcast service with full link upgrade
- 24 个必须掌握的数据库面试问题!
- Common regular expressions - tool classes (mobile number, email, QQ, fax)
- Solidworks渲染技巧如何不显示边线--显示样式设定
- Grain Mall - High Availability Cluster
- Unity使用SteamVRPlugin时如何不让其他Camera位置和旋转收到SteamVRPlugin控制
- openresty 概述
猜你喜欢

Machine learning deep neural network -- Experimental Report

April 13, 2021 interview with beaver family

.net中,日志组件 Nlog,SerialLog, Log4Net的用法

FastRCNN
![Compréhension approfondie de l'expérience de port série stm32 (registre) [Tutoriel de niveau nounou]](/img/b2/f09e220918a85b14a1993aa85f7720.png)
Compréhension approfondie de l'expérience de port série stm32 (registre) [Tutoriel de niveau nounou]

Win10 start FTP service and set login authentication

机器学习PCA——实验报告

Solidworks渲染技巧如何不显示边线--显示样式设定

QT connection MySQL data query failed

Machine Learning Clustering - Experimental Report
随机推荐
FasterRCNN
[Beiyou orchard microprocessor design] 10 serial communication serial communication notes
MySQL backup and restore command
Nacos2.x.x start error creating bean with name 'grpcclusterserver';
MySQL Performance Monitoring and SQL statements
Redis knowledge mind map
02-Redis数据结构之链表
Easyx----- C language implementation 2048
Win10 start FTP service and set login authentication
Laravel admin login add graphic verification code
Is it safe to open an account in the school of Finance and business?
laravel 写原生SQL语句
近期工作汇报
ISO 26262之——2功能安全概念
UDP flood attack defense principle
工作汇报(2)
Laravel admin hidden button, and set button display, default sequence, form form form non modifiable value
Jasperreports - print PDF (project tool)
FastRCNN
工作汇报(3)
























