1. 在阿里云上面搜索短信服务
2. 可以采用测试短信服务
3. 官网代码
@Slf4j
public class Sample {
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,您的 AccessKey ID
.setAccessKeyId(accessKeyId)
// 必填,您的 AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
public static void sendCode(String accessKeyId,String accessKeySecret,String phone,String code) throws Exception {
com.aliyun.dysmsapi20170525.Client client = Sample.createClient(accessKeyId, accessKeySecret);
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
.setSignName("阿里云短信测试")
.setTemplateCode("SMS_154950909")
.setPhoneNumbers(phone)
.setTemplateParam("{\"code\":\""+code+"\"}");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
try {
// 复制代码运行请自行打印 API 的返回值
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
Integer statusCode = sendSmsResponse.statusCode;
log.info(""+statusCode);
} catch (TeaException error) {
// 错误 message
System.out.println(error.getMessage());
// 诊断地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
} catch (Exception _error) {
TeaException error = new TeaException(_error.getMessage(), _error);
// 错误 message
System.out.println(error.getMessage());
// 诊断地址
System.out.println(error.getData().get("Recommend"));
com.aliyun.teautil.Common.assertAsString(error.message);
}
}
}
4. 发送验证码代码
@PostMapping("/sendMsg")
public R<String> sendMsg(HttpServletRequest request, @RequestBody User user) throws Exception {
// 给该手机发送验证码
Sample.createClient("","");
// 随机验证码
Random random = new Random();
int codeNum = random.nextInt( 8998)+1001;
String code =""+codeNum;
// 将验证码存储在session中
Sample.sendCode("","",user.getPhone(),code);
request.getSession().setAttribute(user.getPhone(),code);
return R.success("验证码发送成功");
}
user里面包括手机号,将手机号和accessKeyId,accessKeySecret传入,获得验证码后将验证码放在session里面
5. 登录注册服务
@PostMapping("/login")
public R<User> login(HttpServletRequest request,@RequestBody Map<String,String> map){
String phone = map.get("phone");
String code = map.get("code");
// 判断验证码和手机号是否正确
String oldCode = (String) request.getSession().getAttribute(phone);
if(!oldCode.equals(code)){
return R.error("验证码错误");
}
// 判断是否注册过
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getPhone,phone);
User user = userService.getOne(queryWrapper);
if(user!= null){
request.getSession().setAttribute("user",phone);
return R.success(user);
}
// 注册手机号
User newUser = new User();
newUser.setPhone(phone);
userService.save(newUser);
// 将数据放在session里面,避免重复登录
request.getSession().setAttribute("user",phone);
return R.success(newUser);
}
前端传入手机号和填入的验证码,先判断验证码和session里面的验证码是否一样,如果一样则判断是否注册过,没有注册就先去注册,注册过直接登录,将登录信息放在session里面。