手把手教你从阿里云到idea的短信服务怎么玩.给你女朋友发一条是不是贼帅?

    技术2022-07-10  93

    短信服务

    1.1打开阿里云

    创建成功就会有

    总结阿里云:

    1.开启子用户

    2.新建用户组(设置添加权限)

    3.创建一个用户(具体用来操作的账号)

    4,得到AccessKey(id,密码)

    == 注意:保存账号 ==如果泄露 ,用户组下禁用或者删除

    2.开通阿里云短信服务

    开通后

    申请提交即可

    整理步骤

    1.找到短信控制台面板

    2.找到帮助文档

    https://help.aliyun.com/product/44282.html?spm=5176.12212976.0.0.12791cbeXdF3WC

    3.添加短信模板,等待审核通过

    3.编写 代码 测试

    打开帮助文档,点击javaSDK

    java maven依赖

    <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.1</version> </dependency>

    1.新建spring-boot项目

    2.pom文件中导入依赖

    <!--阿里短信服务--> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>

    3.测试是否导入成功

    复制代码到测试test

    所有的导入都和阿里云相关的

    package com.example.alibabanote; import com.alibaba.fastjson.JSONObject; 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.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import java.util.HashMap; import java.util.Map; @SpringBootTest class AlibabaNoteApplicationTests { @Test void contextLoads() { //连接阿里云 DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "AccessKeyId", "secret"); 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", "电话号你的"); //对应手机号 request.putQueryParameter("SignName", "签名"); //签名,写阿里云签名管理中自己申请的签名, request.putQueryParameter("TemplateCode", "模板号"); //模板 写阿里云模板管理中自己申请的模板Code, //构建短信验证码 HashMap<Object, Object> map = new HashMap<>(); map.put("code",1011); request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map)); System.out.println(map); try { CommonResponse response = client.getCommonResponse(request); System.out.println(response.getData()); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { e.printStackTrace(); } } }

    拿到后写入代码

    4.层

    sends接口
    package com.example.alibabanote.service; import java.util.Map; /** * @author LZP * @create 2020/6/18 */ public interface Sends { public boolean send(String phoneNum, String templateCode, Map<String,Object>code); }
    impl
    package com.example.alibabanote.service.impl; import com.alibaba.fastjson.JSONObject; 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 com.example.alibabanote.service.Sends; import org.springframework.stereotype.Service; import java.util.HashMap; import java.util.Map; /** * @author LZP * @create 2020/6/18 */ @Service public class SendImpl implements Sends { @Override public boolean send(String phoneNum, String templateCode, Map<String, Object> code) { //连接阿里云 DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "AccessKeyId", "secret"); 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", "电话号你的"); //对应手机号 request.putQueryParameter("SignName", "签名"); //签名,写阿里云签名管理中自己申请的签名, request.putQueryParameter("TemplateCode", "模板号"); //模板 写阿里云模板管理中自己申请的模板Code, //构建短信验证码 HashMap<Object, Object> map = new HashMap<>(); map.put("code",1011); request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code)); System.out.println(map); 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; } }
    controller层
    package com.example.alibabanote.controller; import com.example.alibabanote.service.Sends; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ValueOperations; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.UUID; import java.util.concurrent.TimeUnit; /** * @author LZP * @create 2020/6/18 */ @RestController @CrossOrigin //跨域 public class SmsApiController { @Autowired private Sends sends; @Autowired private RedisTemplate<String,String> redisTemplate; @GetMapping("/send{phone}") public String code(@PathVariable("phone") String phone){ //调用发送方法(模拟则很难是·真实业务redis) String code = redisTemplate.opsForValue().get(phone); if (!StringUtils.isEmpty(code)) { return phone + ":" + code + "已存在,还未过期"; } //生成验证码并存入redis中 code = UUID.randomUUID().toString().substring(0, 4); HashMap<String, Object> parap = new HashMap<>(); parap.put("code",code); boolean isSend = sends.send(phone, "模板号", parap);//手机号 if (isSend) { redisTemplate.opsForValue().set(phone,code,5, TimeUnit.SECONDS); return phone + ":" + code + "发送成功!"; }else { return "发送失败"; } } }
    Processed: 0.015, SQL: 9