阿里雲短信驗證碼 - 使用樣例


阿里雲短信驗證碼 - 使用樣例

發送短信驗證碼的流程:

image-20201116090449776

流程說明:

  • 用戶向SSO系統發送請求,在請求中傳遞手機號;
  • SSO系統接收到請求后,生成隨機驗證碼以及短信內容,請求阿里雲短信服務;
  • 阿里雲短信服務接收到請求后,會進行一系列的驗證,比如賬號余額、短信模板是否正確等,最后向運營商發起請求;
  • 運營商接收到請求后,向該手機號下發短信,用戶即可收到短信;

3.6.1、阿里雲短信服務

3.6.1.1、申請簽名與模板

https://dysms.console.aliyun.com/dysms.htm?spm=5176.12818093.0.ddysms.2a4316d0ql6PyD

image-20201016193819097

說明:申請簽名時,個人用戶只能申請一個並且簽名的名稱必須為“ABC商城”,否則審核不通過。

申請模板:

image-20201016193927924

審核時間需要12小時,請耐心等待

3.6.1.2、設置用戶權限

在阿里雲中,需要在RAM服務中創建用戶以及權限,才能通過api進行訪問接口。

image-20201016194331790

創建用戶:

image-20201016194409024

image-20201016195011698

創建完成后要保存AccessKey Secret和AccessKey ID,AccessKey Secret只顯示這一次,后面將不再顯示。

添加權限:

image-20201016194636739

3.6.1.3、示例代碼

文檔:https://help.aliyun.com/document_detail/101414.html?spm=a2c4g.11186623.6.625.18705ffa8u4lwj:

package com.tanhua.sso.service;

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;
/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.5.3</version>
</dependency>
*/
public class SendSms {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
                "LTAI4G7d2Q9CHc741gighjTF", "uKOOGdIKvmoGhHlej8cJY8H3nlU6Fj");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", "158****7944"); //目標手機號
        request.putQueryParameter("SignName", "ABC商城"); //簽名名稱
        request.putQueryParameter("TemplateCode", "SMS_204756062"); //短信模板code
        request.putQueryParameter("TemplateParam", "{\"code\":\"123456\"}");//模板中變量替換
        try {
            CommonResponse response = client.getCommonResponse(request);

            //{"Message":"OK","RequestId":"EC2D4C9A-0EAC-4213-BE45-CE6176E1DF23","BizId":"110903802851113360^0","Code":"OK"}
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM