短信服务
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", "模板号");
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
;
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
;
@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", "模板号");
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
;
@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
){
String code
= redisTemplate
.opsForValue().get(phone
);
if (!StringUtils
.isEmpty(code
)) {
return phone
+ ":" + code
+ "已存在,还未过期";
}
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 "发送失败";
}
}
}