接入阿里短信发送接口:
1、直接去阿里云【云市场】搜索【短信】随便选一个试用测试就行
打开后有相应的api说明
调用地址以及请求参数,可以自己去postman调试或者使用自带的【调试工具:去调试】。
注意:使用postman进行调试时,不要忘记加上appcode
打开文档看就行。
APPCODE在云市场所购买的服务列表里:
2、整合java
整合java的时候,直接往下翻,找到对应的java示例代码:
直接复制里边代码,去测试发送。
@Testvoid 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 83359fd73fe94948385f570e3c139105headers.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 83359fd73fe94948385f570e3c139105headers.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();}}
}
再将刚才我们所定义的组件注入到测试类中看一下效果:
@AutowiredsmsComponent smsCode;@Testvoid 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