目录
打开阿里云:(绑定手机号码),不用申请模板和签名
手机验证码注册流程:
1.客户端输入手机号码,服务端接收手机号码(加上手机号是否已经注册的判断),并生成四位数的验证码。
2.将手机号和生成的验证码传到阿里云短信验证服务端
3.根据阿里云服务端的返回类型判断是否发送成功
4.发送成功,就将手机号作为key,验证码作为value存入redis中(可以设置有效时间);发送失败就返回验证码发送失败提示。
5.客户端收到验证码后,输入验证码后,提交
6.服务端获取验证码和手机号,与redis中存入的手机号和验证码进行比对,对比成功,将用户信息添加到数据库,返回注册成功(也可以直接返回token,跳转登录首页),否则返回注册失败。
实现流程
创建springboot工程,添加依赖
pom.xml:
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</dependency>
</dependencies>
集成redis查看:https://blog.csdn.net/sjgllllll/article/details/122476431
编写applicatioin配置文件
# 服务端口
server.port=8005
# 服务名
spring.application.name=service-msm
# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/guli?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.redis.host=IP地址
spring.redis.port=6379
spring.redis.database= 0
spring.redis.password=密码
spring.redis.timeout=1800000
spring.redis.lettuce.pool.max-active=20
spring.redis.lettuce.pool.max-wait=-1
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-idle=5
spring.redis.lettuce.pool.min-idle=0
#最小空闲
#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
编写controller,根据手机号发送短信
import com.atguigu.cmsservice.service.MsmService;
import com.atguigu.cmsservice.utils.RandomUtil;
import com.atguigu.commonutils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* @description: TODO
* @author 孙
* @date 2022/3/4 10:32
* @version 1.0
*/
@RestController
@RequestMapping("/api/msm")
@CrossOrigin//跨域
public class MsmApiController {
@Autowired
private MsmService msmService;
@Autowired
private RedisTemplate<String,String> redisTemplate;
@GetMapping("/send/{phone}")
public R code(@PathVariable String phone){
String code=redisTemplate.opsForValue().get(phone);
//禁止重复发送
if (!StringUtils.isEmpty(code)){
return R.ok();
}
code=RandomUtil.getFourBitRandom();
Map<String,Object> param=new HashMap<>();
param.put("code",code);
//调用service发送短信的方法
boolean isSend=msmService.send(param,phone);
if (isSend){
//发送成功把验证码放到redis里面
//设置有效时间
redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
return R.ok();
}else{
return R.error().message("短信发送失败");
}
}
}
编写service:
public interface MsmService {
boolean send(Map<String, Object> param, String phone);
}
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.atguigu.cmsservice.service.MsmService;
import org.apache.commons.codec.language.bm.Rule;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Map;
/**
* @description: TODO
* @author 孙
* @date 2022/3/4 10:36
* @version 1.0
*/
@Service
public class MsmServiceImpl implements MsmService {
@Override
public boolean send(Map<String, Object> param, String phone) {
//判断手机是否为null
if (StringUtils.isEmpty(phone)){
return false;
}
//第一个参数默认,第二参数accesskeyid 第三个参数 accesskeysecret
DefaultProfile profile = DefaultProfile.getProfile("default",
"xxxxx", "xxxx");
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",phone); //手机号
request.putQueryParameter("SignName","XXX"); //申请阿里云 签名名称
request.putQueryParameter("TemplateCode","XXX"); //申请阿里云 模板code
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param)); //验证码数据,转换json数据传递
try {
//最终发送
CommonResponse response = client.getCommonResponse(request);
boolean success = response.getHttpResponse().isSuccess();
return success;
}catch(Exception e) {
e.printStackTrace();
return false;
}
}
}
测试:
返回值:
redis保存值:
整合成功