目录
手机号——一键注册登录流程
构建流程图
用户登录流程
前后端都需要加上60s限制,因为有些用户可能会跳过前端,直接请求我们的后端。
手机号一键注册登录流程图
传入的参数对应的就是我们的输入域。
腾讯云短信
我们这里是需要有自己的一个公众号的。
SpringBoot集成腾讯云短信
我们首先要集成sdk,sdk是一个我们发送短信的jar包的依赖:
版本号这里是第三方的云厂商依赖,是锁死的,我们不会去做维护也不会去管理。
接着要去配置密钥:
创建对应的一个映射类,来获取对应的值:
package com.imooc.utils;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@Data
@PropertySource("classpath:tencentcloud.properties")
@ConfigurationProperties(prefix = "tencent.cloud")
public class TencentCloudProperties {
private String secretId;
private String secretKey;
}
@PropertySource("classpath:tencentcloud.properties")这里是指资源文件映射的内容,目录:
发送短信的代码:
package com.imooc.utils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SMSUtils {
@Autowired
private TencentCloudProperties tencentCloudProperties;
public void sendSMS(String phone, String code) throws Exception {
try {
/* 必要步骤:
* 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
* 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
* 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
* 以免泄露密钥对危及你的财产安全。
* CAM密匙查询获取: https://console.cloud.tencent.com/cam/capi*/
Credential cred = new Credential(tencentCloudProperties.getSecretId(),
tencentCloudProperties.getSecretKey());
// 实例化一个http选项,可选的,没有特殊需求可以跳过
HttpProfile httpProfile = new HttpProfile();
// httpProfile.setReqMethod("POST"); // 默认使用POST
/* SDK会自动指定域名。通常是不需要特地指定域名的,但是如果你访问的是金融区的服务
* 则必须手动指定域名,例如sms的上海金融区域名: sms.ap-shanghai-fsi.tencentcloudapi.com */
httpProfile.setEndpoint("sms.tencentcloudapi.com");
// 实例化一个client选项
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
// 实例化要请求产品的client对象,clientProfile是可选的
SmsClient client = new SmsClient(cred, "ap-nanjing", clientProfile);
// 实例化一个请求对象,每个接口都会对应一个request对象
SendSmsRequest req = new SendSmsRequest();
String[] phoneNumberSet1 = {"+86" + phone};//电话号码
req.setPhoneNumberSet(phoneNumberSet1);
req.setSmsSdkAppId(" "); // 短信应用ID: 短信SdkAppId在 [短信控制台] 添加应用后生成的实际SdkAppId
req.setSignName(" "); // 签名
req.setTemplateId(" "); // 模板id:必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看
/* 模板参数(自定义占位变量): 若无模板参数,则设置为空 */
String[] templateParamSet1 = {code};
req.setTemplateParamSet(templateParamSet1);
// 返回的resp是一个SendSmsResponse的实例,与请求对象对应
SendSmsResponse resp = client.SendSms(req);
// 输出json格式的字符串回包
// System.out.println(SendSmsResponse.toJsonString(resp));
} catch (TencentCloudSDKException e) {
System.out.println(e.toString());
}
}
// public static void main(String[] args) {
// try {
// new SMSUtils().sendSMS("18812345612", "7896");
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
}
其中短信应用id,签名,模板id都需要根据自己的填写。
最后可以做一个测试:
运行会报错,因为所处的是一个springboot环境。
我们在HelloController中测试:
在接口文档中进行调试:
这里已经可以成功的进行短信发送了。
完善发送短信接口
接下来我们要整合我们的前端,在前端输入手机号,获得我们的验证码:
首先创建一个PassportController通行证Controller:
注:我们这里的路由地址要与前端匹配,并且要看清楚用的什么方法
为了获得用户ip,我们要在添加一个获得ip的工具类,
package com.imooc.controller;
import com.imooc.grace.result.GraceJSONResult;
import com.imooc.model.Stu;
import com.imooc.utils.IPUtil;
import com.imooc.utils.SMSUtils;
import com.tencentcloudapi.ie.v20200304.models.MuxInfo;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@Slf4j
@Api(tags = "PassportController 通行证接口模块")
@RequestMapping("passport")
@RestController
public class PassportController {
@Autowired
private SMSUtils smsUtils;
@PostMapping("getSMSCode")
public Object getSMSCode(@RequestParam String mobile,
HttpServletRequest request) throws Exception {
// 判断是否为空,为空什么信息都不返回
if(StringUtils.isBlank(mobile)){
return GraceJSONResult.ok();
}
//
//TODO 获取用户id
String userIp = IPUtil.getRequestIp(request);
//TODO 根据用户ip进行限制,限制用户在60s之内只能获得一次验证码
// 获取6位随机验证码
String code = (int)((Math.random()*9+1)*100000)+"";
smsUtils.sendSMS(mobile,code);
log.info(code);
//TODO 把验证码放入redis中,用于后续验证
return GraceJSONResult.ok();
}
}
检查前后端端口是否一致,接着重新启动,在我们的基座中去获取短信:
在我们的控制台中会打印出来一条短信:
这里就完成了我们的集成。