阿里短信服務

初步的一些阿里短信需要的一些東西  官網都有說明

記得吧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<Boolean> getSend(@PathVariable String phone, HttpServletRequest req) throws Exception{
        ApiResult<Boolean> 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 = "17621101265";
    private static String accessKeyId = "LTAIR2VFVKUS9KKM";
    private static String accessKeySecret = "83odNT7J0QifnGwjf6NeSt94GvG88n";
    private static String signName = "滑縣農業";
    private static String templeteCode = "SMS_147971212";

    // 調用短信接口
    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());

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章