开通短信验证码服务及封装工具类

短信验证码的使用步骤:

1.开通短信服务(以阿里云中开通为例)
(1)打开阿里云:
官网网址:https://www.aliyun.com/

(2)点击云市场进入
在这里插入图片描述

(3)点击API模块–API接口平台进入
在这里插入图片描述
(4)点击短信接口,这里除了短信接口还有身份证认证之类的都在这里。也可以选择下面其他的短信接口服务,都大同小异。
在这里插入图片描述在这里插入图片描述 (5)选择一个购买或是直接白嫖几次。选择下面的代码部分,复制导入自己的项目中。以第一个为例:

public static void main(String[] args) {
     String host = "https://smssend.shumaidata.com";
     String path = "/sms/send";
     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("receive", "188****1212");
     querys.put("tag", "123456");
     querys.put("templateId", "M4F8845237");
     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();
     }
 }

导入项目中会发现好多爆红!!不慌,看try…catch中的注释,根据提示去指定地址导入相应的代码和包就好了。
注意:
1.templateID一般个人联系客服都不给开通。直接使用提供的测试的就行。
2.appcode在阿里云开通的服务就可以看到,直接复制就行。

2.将短信方法封装成工具方法

代码如下:(springboot)

package com.zhang.messageutil;

import com.zhang.aliyun.api.gateway.demo.util.HttpUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

// 将发送验证码封装成一个工具类
public class SendMessageUtil {
    /**
     * 发送短信验证码
     * @param shost 发送短信代理的服务商地址,通常不用修改
     * @param spath 具体的路径,通常也不需要进行修改
     * @param smethod 请求方式,通常使用post
     * @param sappcode 自己开通短信服务时的APPCODE
     * @param sphone 要发送验证码的手机号
     * @param stemplateId 模板id,这里使用的是测试id
     */
    public static String sendMessageTag(
            @RequestParam("host") String shost,
            @RequestParam("path") String spath,
            @RequestParam("method") String smethod,
            @RequestParam("appcode") String sappcode,
            @RequestParam("phone") String sphone,
            @RequestParam("templateId") String stemplateId){

        // 这个不用更改
        String host = shost;
        String path = spath;
        // 请求方式
        String method = smethod;
        // 开通服务的appcode,在阿里云上就可以看到
        String appcode = sappcode;
        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("receive", sphone);
        // 生成随机的四位验证码
        String tags = "";
        for (int i=0;i<4;i++) {
            String tag = String.valueOf((int)Math.random()*10);
            tags += tag;
        }

        querys.put("tag", tags);
        // 测试模板M09DD535F4
        querys.put("templateId", stemplateId);
        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()));
            return querys.get("tag");
        } catch (Exception e) {
            e.printStackTrace();
            return "发送失败!";
        }
    }
}

对于方法中的多个参数,除了电话号以外,其他的部分都可以直接写在配置文件中,以后直接修改配置文件就行了。非常的银杏=_=。

3.springboot完成配置类,如果没有配置类那么配置文件就不会识别的。

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component // 这里不加component下面的代码会出错
@ConfigurationProperties(prefix = "send.message")
public class SendMessageProperties {

    private String shost = "https://smssend.shumaidata.com";
    private String spath = "/sms/send";
    private String smethod = "POST";
    private String sappcode = "自己的AppCode";
    // 官方提供的测试templateId
    private String stemplateId = "M09DD535F4";

    public SendMessageProperties() {
    }

    public SendMessageProperties(String shost, String spath, String smethod, String sappcode, String stemplateId) {
        this.shost = shost;
        this.spath = spath;
        this.smethod = smethod;
        this.sappcode = sappcode;

        this.stemplateId = stemplateId;
    }

    public String getShost() {
        return shost;
    }

    public void setShost(String shost) {
        this.shost = shost;
    }

    public String getSpath() {
        return spath;
    }

    public void setSpath(String spath) {
        this.spath = spath;
    }

    public String getSmethod() {
        return smethod;
    }

    public void setSmethod(String smethod) {
        this.smethod = smethod;
    }

    public String getSappcode() {
        return sappcode;
    }

    public void setSappcode(String sappcode) {
        this.sappcode = sappcode;
    }

    public String getStemplateId() {
        return stemplateId;
    }

    public void setStemplateId(String stemplateId) {
        this.stemplateId = stemplateId;
    }
}

这里我的参数直接赋值是因为我的配置文件没有识别这个配置类…,找半天问题也没解决。通常是在配置文件中赋值的。

4.在application.yml中给参数赋值

结束!!!!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值