• 35-SpringBoot 短信验证码接口使用


    1.购买短信验证码服务

    登录阿里云官网进行购买:点击进入
    在这里插入图片描述
    我选择第一个进入,然后购买
    在这里插入图片描述

    2.使用说明

    在这里插入图片描述

    我们要修改的参数就以下几个

    • appcode, 这是相当于是使用服务的密钥,可在 控制台-云市场 查看
    • template_id, 这是短信模板编号, 对应不同的短信内容模板
    • phone_number, 接受验证码的手机号
    • content, 验证码内容, 格式是 code:你的验证码

    3.整合使用

    application.properties

    #短信接口配置
    spring.cloud.alicloud.sms.appcode=d2c9a409391e4f4e9c20gt8d1868eec5
    spring.cloud.alicloud.sms.templateid=TPL_0000
    

    SmsComponent

    package com.atguigu.gulimall.thirdparty.component;
    
    
    import com.atguigu.common.utils.HttpUtils;
    import lombok.Data;
    import org.apache.http.HttpResponse;
    import org.apache.http.util.EntityUtils;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    import java.util.HashMap;
    import java.util.Map;
    
    @ConfigurationProperties(prefix = "spring.cloud.alicloud.sms")
    @Data
    @Component
    public class SmsComponent {
    
        private String templateid;
        private String appcode;
    
        public void sendSmsCode(String phone,String code){
            String host = "https://dfsns.market.alicloudapi.com";
            String path = "/data/send_sms";
            String method = "POST";
            //String appcode = "你自己的AppCode";
            Map<String, String> headers = new HashMap<String, String>();
            //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
            headers.put("Authorization", "APPCODE " + appcode);
            //根据API的要求,定义相对应的Content-Type
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            Map<String, String> querys = new HashMap<String, String>();
            Map<String, String> bodys = new HashMap<String, String>();
            bodys.put("content", "code:"+code);
            bodys.put("phone_number", phone);
            bodys.put("template_id", templateid);
    
    
            try {
                /**
                 * 重要提示如下:
                 * HttpUtils请从
                 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
                 * 下载
                 *
                 * 相应的依赖请参照
                 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
                 */
                HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
                System.out.println(response.toString());
                //获取response的body
                //System.out.println(EntityUtils.toString(response.getEntity()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    

    SmsSendController

    package com.atguigu.gulimall.thirdparty.controller;
    
    
    import com.atguigu.common.utils.R;
    import com.atguigu.gulimall.thirdparty.component.SmsComponent;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RequestMapping("/sms")
    public class SmsSendController {
    
    
        @Autowired
        SmsComponent smsComponent;
    
        /**
         * 提供给别的服务进行调用
         * @param phone
         * @param code
         * @return
         */
        @GetMapping("/sendcode")
        public R sendCode(@RequestParam("phone") String phone, @RequestParam("code") String code){
            smsComponent.sendSmsCode(phone,code);
            return R.ok();
        }
    }
    
    

    4.运行测试

    使用postman测试
    在这里插入图片描述

    成功接收到验证码
    请添加图片描述

  • 相关阅读:
    深入解读Prometheus Adapter:云原生监控的核心组件
    2024现代Android开发趋势
    Angular知识点系列(1)-每天10个小知识
    证书常用相关知识
    ===、==、Object.is 基本包装类型
    玩转UE4/UE5动画系统:UE5的运行时(动态)重定向治好了我的精神内耗
    五分钟了解MES与MOM的区别和联系
    水质查询接口
    【文件上传与包含漏洞综合利用】DVWA-文件上传-难度:High
    JVM知识体系学习一:JVM了解基础、java编译后class文件的类结构详解,class分析工具 javap 和 jclasslib 的使用
  • 原文地址:https://blog.csdn.net/qq_41865229/article/details/127107434