Java 整合阿里云第三方短信服务

前言

这里为什么用阿里云呢?就是因为阿里云的云市场里面有第三方的短信签名不需要企业认证。

流程

1.购买服务

去阿里云云市场找一个第三方的短信服务购买
image

2.debug短信服务

在商品详情里面找到调试工具去调试
image
image
这个APPCODE就是你购买服务后,在你的控制台里面有这个东西,然后发送请求时带上即可。

测试发送
image
image

3.调用短信api接口

	public static void main(String[] args) {
	    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", "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();
	    }
	}

这里直接用它的提供的文档即可,按照提示把github上的代码把HttpUtils搞到本地。
image

测试发送短信,也是成功的。

4.抽取成一个组件,方便调用

@Data
@Component
@ConfigurationProperties(prefix = "spring.cloud.alicloud.sms")
public class SmsComponent {

    public String host;
    public String path;
    public String appcode;

    public String smsSignId;

    public String templateId;

    public void sendSms(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**:1");
        querys.put("smsSignId", smsSignId);
        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();
        }

    }

}

读取配置文件里的配置
image

image

测试也是可以通过的。

posted @ 2022-12-06 12:32  长情c  阅读(266)  评论(0)    收藏  举报