阿里云手机短信验证

一 pom.xml中添加阿里云的依赖

<dependency>
   <groupId>com.aliyun</groupId>
   <artifactId>aliyun-java-sdk-core</artifactId>
   <version>4.5.16</version>
</dependency>

代码

package io.renren.phone;

import cn.hutool.json.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

import java.util.Random;

public class SendMsgUtils {
    //访问凭证管理的 AccessKey ID
    private static final String accessKeyId="";
    //访问凭证管理的 AccessKey Secret
    private static final String accessSecret="";
    //短信签名名称
    private static final String signName="";
    //短信模板code
    private static final String templateCode="";
    //服务地址
    private static final String regionId="cn-hangzhou";
    //版本号为API版本号
    private static final String version="2017-05-25";
    // 访问的域名
    private static final String doMain="dysmsapi.aliyuncs.com";
    private static final String action="SendSms";



    public static String sendMsg(String phone){
        DefaultProfile profile = DefaultProfile.getProfile(regionId,accessKeyId,accessSecret);

        IAcsClient client = new DefaultAcsClient(profile);

        //设置发送短信需要的参数
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(doMain);
        request.setSysVersion(version);
        request.setSysAction(action);
        request.putQueryParameter("RegionId",regionId);
        request.putQueryParameter("PhoneNumbers",phone);
        request.putQueryParameter("SignName",signName);
        request.putQueryParameter("TemplateCode",templateCode);

        String code = randomCode();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",code);
        //构建一个短信的验证码
        request.putQueryParameter("TemplateParam",jsonObject.toString());

        try {
            //返回的是一个JSON字符串,根据Code(生成的验证码)属性是否为null可以判断请求是否发送成功
            CommonResponse response = client.getCommonResponse(request);
            return code;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return  null;

    }

    //生成六位随机验证码
    public static String randomCode(){
        String sources = "0123456789";
        Random random = new Random();
        StringBuffer flag= new StringBuffer();
        // 验证码有几位就循环几次
        for (int j=0;j<6;j++){
            flag.append(sources.charAt(random.nextInt(9))+"");
        }

        return  flag.toString();
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值