• 引入短信服务


    一、阿里云短信服务

    进入阿里云平台,然后选择短信服务,通过API发送短信(需要充值金额,几块钱就可以,我们仅仅是小规模项目)

    找到openAPI

    可以看到Java语言的代码模板,这个就是Java SendSMS短信服务的代码

    创建Accesskey

     创建完成后

    创建用户

    添加权限

    添加签名和模板

    二、后端代码部分

    pom.xml文件中下载依赖

    1. <!-- 阿里云第三方依赖-->
    2. <dependency>
    3. <groupId>com.aliyun</groupId>
    4. <artifactId>aliyun-java-sdk-core</artifactId>
    5. <version>4.5.0</version>
    6. </dependency>

    配置文件中配置参数

    1. aliyun.accessKeyID=用自己的!!!
    2. aliyun.accessSecret=用自己的!!!

    SendSMS工具类

    1. package com.like.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.google.gson.Gson;
    11. //import java.util.*;
    12. //import com.aliyuncs.dysmsapi.model.v20170525.*;
    13. import org.springframework.beans.factory.annotation.Value;
    14. import org.springframework.stereotype.Component;
    15. /*
    16. pom.xml
    17. <dependency>
    18. <groupId>com.aliyun</groupId>
    19. <artifactId>aliyun-java-sdk-core</artifactId>
    20. <version>4.6.0</version>
    21. </dependency>
    22. */
    23. @Component
    24. public class SMSUtils {
    25. @Value("${aliyun.accessKeyID}")
    26. private String accessKeyID;
    27. @Value("${aliyun.accessSecret}")
    28. private String accessKeySecret;
    29. public void sendSMS(String mobile,String code){
    30. // Please ensure that the environment variables ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.
    31. DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
    32. IAcsClient client = new DefaultAcsClient(profile);
    33. CommonRequest request = new CommonRequest();
    34. request.setSysMethod(MethodType.POST);
    35. request.setSysDomain("dysmsapi.aliyuncs.com");
    36. request.setSysVersion("2017-05-25");
    37. request.setSysAction("SendSms");
    38. request.putQueryParameter("RegionId", "cn-hangzhou");
    39. request.putQueryParameter("PhoneNumbers",mobile);
    40. request.putQueryParameter("SignName","keke");
    41. request.putQueryParameter("TemplateCode","SMS_288145524");
    42. request.putQueryParameter("TemplateParam","{\"code\":\"" + code + "\"}");
    43. try {
    44. CommonResponse response = client.getCommonResponse(request);
    45. System.out.println(response.getData());
    46. } catch (ServerException e) {
    47. e.printStackTrace();
    48. } catch (ClientException e) {
    49. e.printStackTrace();
    50. }
    51. }
    52. }

  • 相关阅读:
    java学习--day24(单例模式&序列化&Lambda表达式)
    Springboot中slf4j日志的简单应用
    【pytorch10】统计属性
    依赖注入有几种实现方式?
    域渗透 | kerberos认证及过程中产生的攻击
    Stable diffusion的一些参数意义及常规设置
    如何解决:navicat中Redis(本地主机:6379):连接被拒绝
    Openssl数据安全传输平台009:加密理论基础:哈希/非对称加密RSA/对称加密AES
    MySQL存储引擎
    MybatisPlus(5)
  • 原文地址:https://blog.csdn.net/m0_63732435/article/details/133563063