当前位置:网站首页>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)阿里云短信服务的风控功能;
边栏推荐
- MySQL backup and restore command
- Wangeditor uploading local video modification
- PC QQ大廳 上傳更新 修改versionInfo
- FastRCNN
- Using reflection to export entity data to excel
- Unity使用SteamVRPlugin时如何不让其他Camera位置和旋转收到SteamVRPlugin控制
- 工作汇报(2)
- 互联网对抗神器之漏洞扫描与反渗透
- Machine learning deep neural network -- Experimental Report
- 动态规划解决股票问题(下)
猜你喜欢

10 years' experience in programmer career - for you who are confused

机器学习LDA——实验报告

机器学习SVM——实验报告

Easyexcel - Excel read / write tool

滑动窗口

Redis (basic) - learning notes

Implementing MySQL master-slave replication in docker

PC QQ大廳 上傳更新 修改versionInfo

4、 Stacks and queues

How does unity prevent other camera positions and rotations from being controlled by steamvrplugin when using steamvrplugin
随机推荐
dd命令测试华为鲲鹏&宏衫固态存储磁盘读写速度
JS take the date of the previous month 【 pit filling 】
携程机票 App KMM 跨端 KV 存储库 MMKV-Kotlin | 开源
C language -- operators and expressions
【深度学习理论】(6) 循环神经网络 RNN
Character sets and comparison rules
Compréhension approfondie de l'expérience de port série stm32 (registre) [Tutoriel de niveau nounou]
The difference between NPM and yarn
Machine learning SVM - Experimental Report
MySQL 30 military regulations
02-Redis数据结构之链表
深圳市福田区支持文化创意产业发展若干措施
Work report (2)
10年程序员职业生涯感悟—写给正在迷茫的你
栖霞市住建局和消防救援大队开展消防安全培训
Easyexcel - Excel read / write tool
Windows and Linux regularly backup MySQL database
指南针软件买股票进行交易安全吗?怎么开户买股票
laravel-admin 非自增ID获取, 及提交隐藏表单
Uncaught reflectionexception: class view does not exist
























