Taught you butt Ali cloud messaging service

Just double XII recently doing a small project need to use SMS, registered 125 new users bought a 5000 text messages, valid for 2 years.
Here Insert Picture Description
So how docking Ali cloud SMS, what you need to do?
Can refer https://help.aliyun.com/document_detail/59210.html

SMS flowchart

SMS using the procedure below:
Here Insert Picture Description

Ali cloud settled, Ali cloud registry entries: Click on the registration page, then Verified

SMS service launched

1. opened SMS service: Click SMS service launched

2. Enter the console: Click to view the console page

Here Insert Picture Description

Get AccessKey

1 Create AccessKey: Click Create AccessKey

2. Obtain AccessKey ID and AccessKey Secret: Click AccessKey ID and AccessKey Secret

And create a signature template

Here Insert Picture Description
1. Create a signature before viewing messages rules and audit rules: SMS business rules , audit rules

2. Create a text signature: Click Create a text signature

Need to be reviewed, about two hours.
Here Insert Picture Description

3. Create a text template: Click Create a text template

Note that the template can contain only a placeholder. as follows. Need to be reviewed, about two hours. Remember template Code, such SMS_181918946as: . Here is the call interface required parameters.

Your verification code is $ {code}, the verification code valid for 3 minutes. Code only he knew, killed did not tell anyone Oh!

Here Insert Picture Description

SMS Interface Configuration

1. Set Access Control: Click Access Control

2.SDK and DEMO Download: Download SDK and DEMO

3.API Documents: Click to view the API documentation Introduction

Creating AccessKey

Access key AccessKey (AK) corresponds to the password, but using different scenarios. AccessKey used to programmatically invoke cloud services API, and login password to log into the console. If you do not need to call the API, you do not need to create AccessKey.

Background Information

You can use the AccessKey construct an API request (or using cloud services SDK) to operate the resource. AccessKey including AccessKeyId and AccessKeySecret.

AccessKeyId用于标识用户。
AccessKeySecret是用来验证用户的密钥。AccessKeySecret必须保密。

Warning Do not use the primary account AK, because the principal account AK disclosure of which would threaten the safety of all your resources. Please use the sub-account (RAM user) to operate AK, AK can effectively reduce the risk of leakage.

Steps

Cloud account login RAM console . In personnel management menu on the left navigation bar, click Users. In the user login name / display name list, click the name of the target user RAM. AccessKey in the user area, click Create a new AccessKey. Required to complete the phone verification code when first created. Click OK.

1, first create user groups, user groups plus rights [SMS management authority];
2, create a user, the user can set the landing permissions, you can have a separate password;
3, the user is added to the user group, the user will have a user rights group settings;

Here Insert Picture Description
4, and the user generates a SecretKey Access Key;
Here Insert Picture Description

After finish the above steps, you can refer to the API documentation sample code, call Ali cloud messaging interface.

I write the code as follows:

package org.linlinjava.litemall.core.notify;

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 org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.JacksonUtil;

import java.util.HashMap;
import java.util.Map;

/*
 * 阿里云短信服务
 */
public class AliyunSmsSender implements SmsSender {
    private final Log logger = LogFactory.getLog(AliyunSmsSender.class);

    private String regionId;
    private String accessKeyId;
    private String accessKeySecret;
    private String sign;

    public String getRegionId() {
        return regionId;
    }

    public void setRegionId(String regionId) {
        this.regionId = regionId;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    @Override
    public SmsResult send(String phone, String content) {
        SmsResult smsResult = new SmsResult();
        smsResult.setSuccessful(false);
        return smsResult;
    }

    @Override
    public SmsResult sendWithTemplate(String phone, String templateId, String[] params) {
        DefaultProfile profile = DefaultProfile.getProfile(this.regionId, this.accessKeyId, this.accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", this.regionId);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", this.sign);
        request.putQueryParameter("TemplateCode", templateId);
    
        String templateParam = "{}";
        if(params.length == 1){
            Map<String, String> data = new HashMap<>();
            data.put("code", params[0]);
            templateParam = JacksonUtil.toJson(data);
        }
        else if(params.length > 1){
            Map<String, String> data = new HashMap<>();
            data.put("code", params[0]);
            for(int i = 1; i < params.length; i++){
                data.put("code" + i, params[i]);
            }
            templateParam = JacksonUtil.toJson(data);
        }
        request.putQueryParameter("TemplateParam", templateParam);

        try {
            CommonResponse response = client.getCommonResponse(request);
            SmsResult smsResult = new SmsResult();
            smsResult.setSuccessful(true);
            smsResult.setResult(response);
            return smsResult;
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        SmsResult smsResult = new SmsResult();
        smsResult.setSuccessful(false);
        return smsResult;
    }
}

Which RegionIdmay be cn-hangzhou、cn-beijinglike; Versionmust be 2017-05-25; other parameters AccessKey和SecretKeyfor the above application.

final effect

Here Insert Picture Description

Published 125 original articles · won praise 8 · views 20000 +

Guess you like

Origin blog.csdn.net/ll837448792/article/details/103503998