1. 程式人生 > >轉:Java實現簡訊驗證碼(阿里雲服務商)

轉:Java實現簡訊驗證碼(阿里雲服務商)

1.先去阿里雲開通簡訊服務:

2.新增模板及簽名:需要稽核,個人賬戶稽核就幾分鐘就OK

先解釋一下模板及簽名:

標準參照:https://help.aliyun.com/document_detail/55324.html?spm=5176.sms-sign.0.0.765c1cbeNhvWBZ

去申請模板及簽名:

3.建立自己的訪問祕鑰(AccessKeyId 與 AccessKeySecret)

參考:https://ak-console.aliyun.com/?spm=a2c4g.11186623.2.5.S2zaID#/accesskey

4.測試程式碼:加入jar包,注意我這裡是最新的3.7.1的包,如果其他工程也有依賴這個包的話,可能會部分功能過時,比如3.7.1的oss檔案上傳會無緣無故報錯,按照官網文件寫,需要把這個包降級到3.5.0,我這裡只是簡訊測試,就用最新的了

<!--簡訊sdk-->
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>3.7.1</version>
</dependency>
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
  <version>1.1.0</version>
</dependency>
package cn.lfungame.test;

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;

/**
 * @Auther: ZouLF
 * @Date: 2018/5/29 10:49
 * @Description: 簡訊驗證碼測試
 */
public class SmsUtils {

    // 產品名稱:雲通訊簡訊API產品,開發者無需替換
    static final String product = "Dysmsapi";
    // 產品域名,開發者無需替換
    static final String domain = "dysmsapi.aliyuncs.com";

    // TODO 此處需要替換成開發者自己的AK(在阿里雲訪問控制檯尋找)
    static final String accessKeyId = "youaccessKeyId";           // TODO 改這裡
    static final String accessKeySecret = "youaccessKeySecret"; // TODO 改這裡



    public static SendSmsResponse sendSms(String telephone, 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-hangzhou", accessKeyId, accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        // 組裝請求物件-具體描述見控制檯-文件部分內容
        SendSmsRequest request = new SendSmsRequest();
        // 必填:待發送手機號
        request.setPhoneNumbers(telephone);
        // 必填:簡訊簽名-可在簡訊控制檯中找到
        request.setSignName("你的簡訊簽名"); // TODO 改這裡
        // 必填:模板CODE-可在簡訊控制檯中找到
        request.setTemplateCode("你的簡訊模板");  // TODO 改這裡
        // 可選:模板中的變數替換JSON串,如模板內容為"親愛的使用者,您的驗證碼為${code}"時,此處的值為
        request.setTemplateParam("{\"code\":\"" + code + "\"}");

        // 選填-上行簡訊擴充套件碼(無特殊需求使用者請忽略此欄位)
        // request.setSmsUpExtendCode("90997");

        // 可選:outId為提供給業務方擴充套件欄位,最終在簡訊回執訊息中將此值帶回給呼叫者
        // request.setOutId("yourOutId");

        // hint 此處可能會丟擲異常,注意catch
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){
                System.out.println("簡訊傳送成功!");
        }else {
                System.out.println("簡訊傳送失敗!");
        }
        return sendSmsResponse;
    }

    //以下為測試程式碼,隨機生成驗證碼
    private static int newcode;
    public static int getNewcode() {
        return newcode;
    }
    public static void setNewcode(){
         newcode = (int)(Math.random()*9999)+100;  //每次呼叫生成一次四位數的隨機數
     }
    public static void main(String[] args) throws Exception {
         setNewcode();
         String code = Integer.toString(getNewcode());
         SendSmsResponse sendSms =sendSms("測試手機號碼",code);//填寫你需要測試的手機號碼
         System.out.println("簡訊介面返回的資料----------------");
         System.out.println("Code=" + sendSms.getCode());
         System.out.println("Message=" + sendSms.getMessage());
         System.out.println("RequestId=" + sendSms.getRequestId());
         System.out.println("BizId=" + sendSms.getBizId());

    }
}

阿里雲簡訊服務錯誤提示參考: