• 19:第三章:开发通行证服务:2:在程序中,打通阿里云短信服务;(仅仅是打通阿里云短信服务器,不涉及具体的业务开发)


    说明:

    (1)本篇博客需要注意的点:

              ● 本篇博客仅仅是打通阿里云短信服务,不涉及【阿里云短信服务,在项目中的具体应用】,也不涉及【具体业务的开发】;

              ● 阿里云短信服务,作为一个第三方的“工具”,我们把其写在了【imooc-news-dev-common】通用工程中;

    目录

    一:购买阿里云短信云服务; 

    二:在【imooc-news-dev-common】通用工程中,获取阿里云的秘钥;

    1.在【imooc-news-dev-common】通用工程中,引入spring相关依赖、阿里云相关依赖;

    2.在【imooc-news-dev-common】通用工程中,创建资源文件:aliyun.properties:保存阿里云短信服务的秘钥;

    3.在【imooc-news-dev-common】通用工程中,创建“获取资源文件信息”的配置类:AliyunResource类:从aliyun.properties中获取秘钥;

    三:在自己的程序中,打通阿里云短信服务;

    1.在【imooc-news-dev-common】通用工程中,创建SMSUtils工具类;(这个工具类的作用,就是发送短信的)

    2.在业务代码中,调用【发送短信的SMSUtils工具类】;

    (1)首先,在【imooc-news-dev-api】接口工程中,定义一个接口;

    (2)然后,在【imooc-news-dev-user】这个用户微服务中,去实现接口;

    3.实际测试;

    四:Summary;


    一:购买阿里云短信云服务; 

    在我们程序中,要想使用这个阿里云短信服务,是需要这个秘钥的;


    二:在【imooc-news-dev-common】通用工程中,获取阿里云的秘钥;

    1.在【imooc-news-dev-common】通用工程中,引入spring相关依赖、阿里云相关依赖;

    1. <!-- 引入SpringBoot 依赖-->
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter</artifactId>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.springframework.boot</groupId>
    8. <artifactId>spring-boot-starter-web</artifactId>
    9. </dependency>
    10. <dependency>
    11. <groupId>org.springframework.boot</groupId>
    12. <artifactId>spring-boot-configuration-processor</artifactId>
    13. </dependency>
    14. <!-- 第三方云厂商相关的依赖,阿里云 -->
    15. <dependency>
    16. <groupId>com.aliyun</groupId>
    17. <artifactId>aliyun-java-sdk-core</artifactId>
    18. <version>4.5.0</version>
    19. </dependency>
    20. </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】去写版本;

              ● 自然,对于有些第三方依赖,我们也想把其定义在顶级父工程中,也是可以的;

    2.在【imooc-news-dev-common】通用工程中,创建资源文件:aliyun.properties:保存阿里云短信服务的秘钥;

    PS:这个配置文件名字,可以随便起;

    3.在【imooc-news-dev-common】通用工程中,创建“获取资源文件信息”的配置类:AliyunResource类:从aliyun.properties中获取秘钥;

    创建com.imooc.utils.extend包,并创建AliyunResource类;

    AliyunResource类:

    1. package com.imooc.utils.extend;
    2. import org.springframework.boot.context.properties.ConfigurationProperties;
    3. import org.springframework.context.annotation.PropertySource;
    4. import org.springframework.stereotype.Component;
    5. @Component //因为,我们类对象,也希望使用IoC管理起来;
    6. @PropertySource("classpath:aliyun.properties") //配置我么配置文件的路径;
    7. @ConfigurationProperties(prefix = "aliyun") //
    8. public class AliyunResource {
    9. private String accessKeyId;
    10. private String accessKeySecret;
    11. public String getAccessKeyId() {
    12. return accessKeyId;
    13. }
    14. public void setAccessKeyId(String accessKeyId) {
    15. this.accessKeyId = accessKeyId;
    16. }
    17. public String getAccessKeySecret() {
    18. return accessKeySecret;
    19. }
    20. public void setAccessKeySecret(String accessKeySecret) {
    21. this.accessKeySecret = accessKeySecret;
    22. }
    23. }

    说明:

    (1)因为,这儿需要使用Spring的内容,所以我们在【common】中引入了spring相关依赖;

    (2)配置类内容说明;通过这个配置类,我们就可以获取aliyun.properties中的阿里云秘钥了;


    三:在自己的程序中,打通阿里云短信服务;

    1.在【imooc-news-dev-common】通用工程中,创建SMSUtils工具类;(这个工具类的作用,就是发送短信的)

    1. package com.imooc.utils;
    2. import com.aliyuncs.CommonRequest;
    3. import com.aliyuncs.CommonResponse;
    4. import com.aliyuncs.DefaultAcsClient;
    5. import com.aliyuncs.IAcsClient;
    6. import com.aliyuncs.exceptions.ClientException;
    7. import com.aliyuncs.exceptions.ServerException;
    8. import com.aliyuncs.http.MethodType;
    9. import com.aliyuncs.profile.DefaultProfile;
    10. import com.imooc.utils.extend.AliyunResource;
    11. import org.springframework.beans.factory.annotation.Autowired;
    12. import org.springframework.stereotype.Component;
    13. @Component
    14. public class SMSUtils {
    15. @Autowired
    16. public AliyunResource aliyunResource;
    17. /**
    18. * 发送短信的方法
    19. * @param mobile:手机号
    20. * @param code:验证码;
    21. */
    22. public void sendSMS(String mobile, String code) {
    23. //首先,构建一个profile,即是一种认证;
    24. DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
    25. aliyunResource.getAccessKeyId(),//获取秘钥id
    26. aliyunResource.getAccessKeySecret());//获取秘钥
    27. //创建一个client,其可以用来发起一个调用请求;
    28. IAcsClient client = new DefaultAcsClient(profile);
    29. //然后,构建一个request请求;
    30. CommonRequest request = new CommonRequest();
    31. request.setSysMethod(MethodType.POST);//调用请求,采用的是POST形式;
    32. request.setSysDomain("dysmsapi.aliyuncs.com");//这儿的domain是个固定的
    33. request.setSysVersion("2017-05-25");//版本号
    34. request.setSysAction("SendSms");//发送的动作,直接固定使用这个即可;
    35. request.putQueryParameter("RegionId", "cn-hangzhou");//设置区域的id,还是杭州的这个
    36. //具体,还要设置短信发送的一些设置;
    37. request.putQueryParameter("PhoneNumbers", mobile);//设置发送的手机号
    38. request.putQueryParameter("SignName", "阿里云短信测试");//短信签名,因为自己的项目目前并没有备案,所以使用的是其测试的
    39. request.putQueryParameter("TemplateCode", "SMS_154950909");//发送短信的模板号;(即发送短信,需要遵守的模板)
    40. request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");//具体的验证码,这儿code需要是一个JSON对象;
    41. try {
    42. //上面,构架好请求后;;就可以直接使用client去发送请求;
    43. CommonResponse response = client.getCommonResponse(request);
    44. System.out.println(response.getData());
    45. } catch (ServerException e) {
    46. e.printStackTrace();
    47. } catch (ClientException e) {
    48. e.printStackTrace();
    49. }
    50. }
    51. }

    说明:

    (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)阿里云短信服务的风控功能;

  • 相关阅读:
    CGAL AABB树
    No theme registered! Use AbpThemingOptions to register themes
    vue实现自动生成路由,非手动创建,含避坑点
    GBase 8a常用命令
    大厂招聘IO常问面试题
    JZ69 跳台阶 JZ71 跳台阶扩展问题 JZ10 斐波那契数列
    Unity入门02——Unity工作原理
    【数据链路层】网络基础 -- MAC帧协议与ARP协议
    数据分析---Python基础
    Servlet小结
  • 原文地址:https://blog.csdn.net/csucsgoat/article/details/125466345