B站小狂神-此博客的内容就是看了这个视频的总结(博主自己写的哦~并非转载)
视频链接-【狂神说】通俗易懂的阿里云短信业务实战教程(露脸)
您是否还在为别人的项目有短信功能自己的却没有?
您是否还在为自己的项目没有短信功能逼格不够高而发愁?
您是否想要简单了解下短信功能但网上的只有代码没有其他相关知识的介绍而发愁?
…
dont 担心了!这篇博客是为了那些想简单入手下短信服务的老哥而准备的~~(大佬勿喷)
主要介绍下阿里云的一些用户操作(直接用最高权限的accessKeyId和key密码也不现实是不是,需要分不同的用户使用),简单介绍下阿里云短信开通,包括添加短信模板和签名(决定短信发送什么是不是)
您也可以点击目录,跳转到您想看的部分去
文章目录
- 1.了解阿里云用户权限操作(多图警告)
- 2.开通阿里云短信服务
- 添加短信模板
- 添加签名
- 3.编写测试代码()
- 项目结构
- 相关的配置文件
- controller,service层代码
- 结果展示
- 4.编写可复用的微服务接口(暂时空着,微服务还没学233)
1.了解阿里云用户权限操作(多图警告)

阿里云账号大家申请之后,就可以使用阿里云的服务了,我们一般都是通过授权码+id+密码的方式来使用阿里云服务的,这次我们就用短信服务来做个小Demo,先登录阿里云官网,按着下面的图步骤来走。



创建一个用户组,给他添加下短信权限,Sms

然后用户也是差不多,

创建一个用户给它添加到刚才那个组里面,这样整个用户就只能使用短信服务了,值得注意的是那个创建用户的时候会告诉accessKeyId和accessKeySecret,记得把这两个保存下来,特别是accessKeySecret只在你创建用户的时候显示,忘了的话只能删除accessKeyId再新建一个了,这两个是等会需要填到代码里面的。老哥们注意下了,上面这些操作都是为了保护你的账号,这个用户组只能使用短信服务的,其他服务是是使用不了的。
(这个授权码和密码有点类似快递小哥进你的小区,你给他发个授权码和密码,有一定的时效或者权限,总比直接告诉他你的账号和密码要安全多了)
2.开通阿里云短信服务

我们先在搜索框中输入短信服务,背景就是我们点击回车之后的啦样子啦,注意那个国内服务,我们等下要设置的什么短信内容都是在这里设置的,看那个签名管理,模板管理。说一说为什么要设置这两个内容,这两个决定你的短信发送是什么内容!

添加短信模板

注意那个模板CODE,必须审核通过才能用在代码中!

再继续完成一些相关的内容补充,点击提交就好了。
添加签名

之后就是签名管理,也是一个类似的操作。这个签名就是说明你是哪个公司或者是什么项目的。我写学习之星是因为某个群里我的外号叫学习之星2333。注意,这个签名也是必须通过了才能在代码中写“学习之星”的!
这两个需要人工审核,不过感觉快很多的,大概20分钟左右吧。
3.编写测试代码()
项目结构
先大概看看项目结构吧,不复杂的,代码中我也会写一些注释方便大家理解。

相关的配置文件
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <!-- 阿里云短信依赖 --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.1.0</version> </dependency> <!--fastjson,用来解析json--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency> <!-- 加入redis相关的依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <exclusions> <exclusion> <groupId>io.lettuce</groupId> <artifactId>lettuce-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> </dependency> </dependencies> |
application.properties
1 2 3 4 5 6 | // 服务端口 server.port=8080 // redis 相关的配置 spring.redis.host=127.0.0.1 spring.redis.port=6379 |
controller,service层代码
service层
SendSms 接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | package com.example.sms.service; import org.springframework.stereotype.Service; import java.util.Map; public interface SendSms { /** * 发送短信验证码 * @param phoneNum 手机号 * @param templateCode 模板号 * @param code 验证码 * @return */ public boolean send (String phoneNum, String templateCode, Map<String, Object> code); } |
SendSmsImpl 实现类
注意替换accessKeyId,accessKeySecret,还有request.putQueryParameter方法中的一些内容!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | /** * 发送短信验证码 * @param phoneNum 手机号 * @param templateCode 模板号 * @param code 验证码 * @return */ @Service public class SendSmsImpl implements SendSms { @Override public boolean send(String phoneNum, String templateCode, Map<String, Object> code) { // 连接阿里云 DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "请替换为你的accessKeyId", "请替换为你的accessKeySecret"); IAcsClient client = new DefaultAcsClient(profile); // 构建请求 CommonRequest request = new CommonRequest(); request.setMethod(MethodType.POST); // 这些内容不要动,是人家阿里爸爸弄出来的,咱不用管 request.setDomain("dysmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); // 自己的内容,此处 SendSms 为发送验证码 request.setAction("SendSms"); //自定义的参数(手机号,验证码,签名,模板! ) //这是我的内容,请结合你的情况修改为你的东西!!! request.putQueryParameter( "PhoneNumbers",phoneNum); request.putQueryParameter( "SignName","学习之星"); request.putQueryParameter( "TemplateCode","SMS_189620774"); request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code)); 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层
PS:像这种注解@GetMapping("/send/{phone}") ,浏览器URL对应的就是http://localhost:8080/send/123456
不要写成了什么 http://localhost:8080/send/phone=15894621650 这样是接受不到的!!
该接口应该使用get方法访问,不是post方法!!(接口测试工具中要注意下)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | @RestController @CrossOrigin //跨域支持 public class SmsApiController { @Autowired private SendSms sendSms; @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> param = new HashMap<>(); param.put("code", code); boolean isSend = sendSms.send(phone, "SMS_189620774", param); if (isSend) { redisTemplate.opsForValue().set(phone, code, 600, TimeUnit.SECONDS); return phone + ":" + code + "发送成功!"; } else { return "发送失败!"; } } } |
结果展示
接口测试工具效果:

手机接收到的效果:

4.编写可复用的微服务接口(暂时空着,微服务还没学233)
结合你的具体业务吧233
谢谢您能看到结尾~
原人生的路上,你我共勉~