阿里短信服务

初步的一些阿里短信需要的一些东西  官网都有说明

记得把AccessKey启用,首次免费送你100条可以测试用

https://help.aliyun.com/document_detail/59210.html?spm=5176.sms-account.108.5.381e1cbefHnojl

 

/**
     * 发送验证码
     * @param phone
     * @param req
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getSend/{phone}",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getSend(@PathVariable String phone, HttpServletRequest req) throws Exception{
        ApiResult result = ApiResult.makeSuccessResult();
        //根据手机后四位 随机唯一验证码生成
        String code = ShareCodeUtil.toSerialCode(Integer.parseInt(phone.substring(phone.length()-4,phone.length())));
        //调用发送方法
        AliMessageSend.sendSms(phone,code);
        //存起来 以方便验证
        req.getSession().setAttribute(phone,code);
        return result;
    }
package com.fx.fxxt.utils.aliMessageSend;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.fx.fxxt.utils.ShareCodeUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author: Yi xiaobai
 * @Date: 2018/10/15 0015 上午 10:03
 */
public class AliMessageSend {
    // 产品名称:云通信短信API产品,开发者无需替换
    private static final String product = "Dysmsapi";
    // 产品域名,开发者无需替换
    private static final String domain = "dysmsapi.aliyuncs.com";

    // 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
    //private static String mobile = "176****1111";
    private static String accessKeyId = "****";
    private static String accessKeySecret = "****";
    private static String signName = "短信测试";
    private static String templeteCode = "SMS_********";

    // 调用短信接口
    public static void main(String[] args) {
        /*try {
            //sendSms();
        } catch (ClientException e) {
            System.out.println(e);
        }*/
    }

    // 发送短信方法
    public static SendSmsResponse sendSms(String phone,String code) throws ClientException {
        // 可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        // 初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-beijing", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-beijing", "cn-beijing", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        // 组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();

        // 必填:待发送手机号
        request.setPhoneNumbers(phone);
        // 必填:短信签名-可在短信控制台中找到
        request.setSignName(signName);
        // 必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templeteCode);

        // 可选:模板中的变量替换JSON串,如模板内容为"尊敬的用户,您的验证码为${code}"时,此处的值为


        String jsonParam = "{\"code\":\""+ code +"\"}";

        request.setTemplateParam(jsonParam);

        // hint 此处可能会抛出异常,注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        return sendSmsResponse;
    }
}

 对比验证码

// 获取当前手机号的验证码  进行验证  然后和 传进来的进行equals 
String code = (String) request.getSession().getAttribute(dto.getPhone());

 

你可能感兴趣的:(java)