【日常记录】接入短信发送功能,为实现完整的注册功能做准备

接入阿里短信发送接口:

1、直接去阿里云【云市场】搜索【短信】随便选一个试用测试就行
在这里插入图片描述
打开后有相应的api说明
在这里插入图片描述
在这里插入图片描述
调用地址以及请求参数,可以自己去postman调试或者使用自带的【调试工具:去调试】。
注意:使用postman进行调试时,不要忘记加上appcode
在这里插入图片描述
打开文档看就行。

APPCODE在云市场所购买的服务列表里:
在这里插入图片描述

2、整合java
整合java的时候,直接往下翻,找到对应的java示例代码:
在这里插入图片描述
直接复制里边代码,去测试发送。

@Test
    void sendSms() {
        String host = "https://gyytz.market.alicloudapi.com";
        String path = "/sms/smsSend";
        String method = "POST";
        String appcode = "您的appcode";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", "手机号");
        querys.put("param", "**code**:12345,**minute**:5");
        querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
        querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");
        Map<String, String> bodys = new HashMap<String, String>();


        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();
        }
    }

测试成功
这里直接将发送验证码服务抽取成一个组件并进行属性绑定(在yml里来配置公共的属性):

@ConfigurationProperties(prefix = "spring.xue.sms")
@Data
@Component
public class smsComponent {
    //将这些属性通过yml来配置
    private String path;
    private String host;
    private String templateId;
    private String appcode;
    public void sendSmsCode(String phone,String code){
        String method = "POST";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", phone);
        querys.put("param", "**code**:"+code+",**minute**:5");
        querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
        querys.put("templateId", templateId);
        Map<String, String> bodys = new HashMap<String, String>();


        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();
        }
    }
}

再将刚才我们所定义的组件注入到测试类中看一下效果:

    @Autowired
    smsComponent smsCode;
    @Test
    void sendSmsCodeTest(){
        smsCode.sendSmsCode("测试手机号","697498");
    }

最终我手机肯定回收到验证码:
在这里插入图片描述
这样短信就接入成功了。

-----------------以下内容请自动略过---------------------------------------------------------

以下是记录一下我的笔记位置(防止遗忘):

短信60s倒计时效果:
D:\JAVA\jdmall\jdmall-auth-server\src\main\resources\templates\register.html

验证码接口:远程调用短信发送服务、防止验证码发送频繁、验证码接口防刷
D:\JAVA\jdmall\jdmall-auth-server\src\main\java\com\xue\jdmall\webController\loginController.java

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值