目录
一、用户注册登录流程
二、使用阿里云发送短信验证码
进入控制台查看购买的短信的详细信息
⭐生成密钥
密钥管理(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;
@Component
classpath:aliyun.properties 注意不要加空格
@PropertySource("classpath:aliyun.properties")
//前缀
@ConfigurationProperties(prefix = "aliyun")
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.google.gson.Gson;
import com.imooc.utils.extend.AliyunResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SMSUtils {
@Autowired
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;
@Api(value = "用户注册登录",tags ={"用于用户注册登录的controller"})
public interface PassportControllerApi {
/**
* 获得短信验证码
* @return
*/
@ApiOperation(value = "获得短信验证码",notes = "获得短信验证码",httpMethod = "GET")
@GetMapping("/getSMSCode")
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;
@RestController
public class PassportController implements PassportControllerApi {
final static Logger logger = LoggerFactory.getLogger(PassportController.class);
@Autowired
private SMSUtils smsUtils;
@Override
public GraceJSONResult getSMSCode() {
String random = "5201314";
smsUtils.sendSMS("17829172045","5201314");
return GraceJSONResult.ok();
}
}
运行主启动类,访问地址,完成短信发送
💧验证码防盗刷监控
如果开启,这也会可能成为测试时验证码发送失败的原因