使用阿里云发送短信验证码
目录
一、用户注册登录流程
二、使用阿里云发送短信验证码
进入控制台查看购买的短信的详细信息
⭐生成密钥
密钥管理(Access key)
点击Access key进入密钥管理界面
点击创建AccessKey
新建一个密钥
⭐将密钥配置到项目中
⚪新建properties文件并填入相应信息
配置的信息分别对应:
可以在生产时下载的文件中直接查看
⚪pom配置
-
<dependency>
-
<groupId>com.aliyun</groupId>
-
<artifactId>aliyun-java-sdk-core</artifactId>
-
<version>4.6.0</version>
-
</dependency>
⚪ 测试代码
- 配置类
-
import org.springframework.boot.context.properties.ConfigurationProperties;
-
import org.springframework.context.annotation.PropertySource;
-
import org.springframework.stereotype.Component;
-
-
-
-
classpath:aliyun.properties 注意不要加空格
-
-
//前缀
-
-
public class AliyunResource {
-
private String accessKeyID;
-
private String accessKeySecret;
-
-
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;
-
}
-
}
- 用官网给的测试代码进行测试
需要注意的参数
-
import com.aliyuncs.DefaultAcsClient;
-
import com.aliyuncs.IAcsClient;
-
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
-
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
-
import com.aliyuncs.exceptions.ClientException;
-
import com.aliyuncs.exceptions.ServerException;
-
import com.aliyuncs.profile.DefaultProfile;
-
import com.谷歌.gson.Gson;
-
import com.imooc.utils.extend.AliyunResource;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Component;
-
-
-
public class SMSUtils {
-
-
public AliyunResource aliyunResource;
-
-
public void sendSMS(){
-
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
-
aliyunResource.getAccessKeyID(),
-
aliyunResource.getAccessKeySecret());
-
IAcsClient client = new DefaultAcsClient(profile);
-
SendSmsRequest request = new SendSmsRequest();
-
request.setSignName("阿里云短信测试");
-
request.setTemplateCode("SMS_154950909");
-
request.setPhoneNumbers("1234567890");
-
request.setTemplateParam("{\"code\": \"" code "\"}");
-
try {
-
SendSmsResponse response = client.getAcsResponse(request);
-
System.out.println(new Gson().toJson(response));
-
} catch (ServerException e) {
-
e.printStackTrace();
-
} catch (ClientException e) {
-
System.out.println("ErrCode:" e.getErrCode());
-
System.out.println("ErrMsg:" e.getErrMsg());
-
System.out.println("RequestId:" e.getRequestId());
-
}
-
}
-
}
⚪添加签名
⚪添加短信发送的模板
- PassportControllerApi
-
import com.imooc.grace.result.GraceJSONResult;
-
import io.swagger.annotations.Api;
-
import io.swagger.annotations.ApiOperation;
-
import org.springframework.web.bind.annotation.GetMapping;
-
-
-
-
public interface PassportControllerApi {
-
/**
-
* 获得短信验证码
-
* @return
-
*/
-
-
-
public GraceJSONResult getSMSCode();
-
}
- PassportController
-
import com.imooc.api.controller.user.PassportControllerApi;
-
import com.imooc.grace.result.GraceJSONResult;
-
import com.imooc.utils.SMSUtils;
-
import org.slf4j.Logger;
-
import org.slf4j.LoggerFactory;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.web.bind.annotation.RestController;
-
-
-
public class PassportController implements PassportControllerApi {
-
-
final static Logger logger = LoggerFactory.getLogger(PassportController.class);
-
-
-
private SMSUtils smsUtils;
-
-
-
public GraceJSONResult getSMSCode() {
-
String random = "5201314";
-
smsUtils.sendSMS("17829172045","5201314");
-
return GraceJSONResult.ok();
-
}
-
}
运行主启动类,访问地址,完成短信发送
💧验证码防盗刷监控
如果开启,这也会可能成为测试时验证码发送失败的原因
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfhceff
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24