開通阿裡雲短信服務

阿裡雲短信服務 1,阿裡雲用戶權限操作 1.1、找到後臺放在個人頭像上面 選擇 AccessKey 管理

1.2、選擇 子用戶

1.3、創建用戶組


1.4、給用戶組添加權限



然後就可以看到你的權限裡面多瞭一個sms的短信權限

1.5、創建用戶


註意!註意!註意點擊確認後隻可以看到一次密碼返回就看不到瞭

註意!註意!註意點擊確認後隻可以看到一次密碼返回就看不到瞭

註意!註意!註意點擊確認後隻可以看到一次密碼返回就看不到瞭

1.6、把用戶加入到用戶組

2、開通阿裡雲短信服務 2.1、搜索短信服務

2.1、添加模板

2.3、添加簽名



3、編寫代碼 3.1、創建一個springboot項目 3.2、導入依賴

<dependency>    <groupId>com.alibaba</groupId>    <artifactId>fastjson</artifactId>    <version>1.2.76</version></dependency><dependency>    <groupId>com.aliyun</groupId>    <artifactId>aliyun-java-sdk-core</artifactId>    <version>4.5.10</version></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId></dependency>

3.3、model

package org.kang.aliyundxfw.model;import lombok.Data;import java.util.Map;/** * @author 康康 * @create 2021-10-30 14:42 * @explain */@Datapublic class SendSms {    /**     * 手機號     */    private String PhoneNumbers;    /**     *模板     */    private final String templateCode="SMS_227260007";    /**     * 簽名     */    private final String SignName="康康科技";    /**     * 驗證碼     */    private Map<String,Object> code;}

3.4、Service

package org.kang.aliyundxfw.service;import org.kang.aliyundxfw.model.SendSms;/** * @author 康康 * @create 2021-10-30 14:39 * @explain */public interface SendSmsService {    boolean send(SendSms sendSms);}

3.4.1、impl

package org.kang.aliyundxfw.service.impl;import com.alibaba.fastjson.JSON;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.kang.aliyundxfw.model.SendSms;import org.kang.aliyundxfw.service.SendSmsService;import org.springframework.stereotype.Service;/** * @author 康康 * @create 2021-10-30 15:07 * @explain */@Servicepublic class SendSmsServiceImpl implements SendSmsService {    @Override    public boolean send(SendSms sendSms) {        //連接阿裡雲 第二個參數是 AccessKey的id 第三個參數是 AccessKey的密碼        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tQHFJoDijGxRYgoDZYM", "LRn4x2XHvwuOCNn6lYWrZxYPROmXsh");        //構建請求        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("PhoneNumbers",sendSms.getPhoneNumbers());        request.putQueryParameter("TemplateCode",sendSms.getTemplateCode());        request.putQueryParameter("TemplateParam", JSON.toJSONString(sendSms.getCode()));        try {            CommonResponse response = client.getCommonResponse(request);            System.out.println(response.getData());            //返回成功還是失敗            return response.getHttpResponse().isSuccess();        } catch (ServerException e) {            e.printStackTrace();        } catch (ClientException e) {            e.printStackTrace();        }        return false;    }}

3.5、contrlloer

package org.kang.aliyundxfw.contrlloer;import io.netty.util.internal.StringUtil;import org.kang.aliyundxfw.model.SendSms;import org.kang.aliyundxfw.service.SendSmsService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.web.bind.annotation.CrossOrigin;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;import java.util.UUID;import java.util.concurrent.TimeUnit;/** * @author 康康 * @create 2021-10-30 15:16 * @explain */@RestController@CrossOrigin //跨域支持public class SendSmsContrlloer {    @Autowired    private SendSmsService sendSmsService;    @Autowired    private RedisTemplate<String, String> redisTemplate;    @GetMapping("/send")    public String code(SendSms sendSms) {        //根據手機號到redis裡面查看有沒有這個手機號        String code = redisTemplate.opsForValue().get(sendSms.getPhoneNumbers());        if (!StringUtil.isNullOrEmpty(code)) {            return sendSms.getPhoneNumbers() + ":" + sendSms.getCode() + "已存在,還沒有過期";        }        //隨機生成四位數的驗證碼        code = UUID.randomUUID().toString().substring(0, 4);        Map<String,Object> map=new HashMap<>();        map.put("code",code);        sendSms.setCode(map);        //調用Service的方法        boolean isSend = sendSmsService.send(sendSms);        if(isSend){            //成功就把手機號放到redis裡面 設置5分鐘過期            redisTemplate.opsForValue().set(sendSms.getPhoneNumbers(),code,5, TimeUnit.SECONDS);            return sendSms.getPhoneNumbers() + ":" + sendSms.getCode() + "發送成功";        }else {            return "發送失敗";        }    }}
本文來自網絡,不代表程式碼花園立場,如有侵權,請聯系管理員。https://www.codegarden.cn/article/28057/
返回顶部