阿里云短信接口实现模板

本文介绍了如何在前端接收到手机号后,通过检查redis缓存来决定是否发送短信验证码。若缓存中无验证码,将生成四位数字验证码,利用阿里云的templateCode发送,并将验证码与手机号存入redis并设定过期时间。
摘要由CSDN通过智能技术生成

1、首先我们需要前端传送的手机号phone,每次请求接口时都去redis缓存中以phone作为key去查有没有对应的code,如果存在,则不发送验证消息;如果code为空,则自动生成一个四位数验证码,调用send()方法,其中“SMS_154161”是我们在阿里云生成的一个templateCode,然后我们吧phone和code以key、value的形式存入我们的redis缓存中,同时设置过期时间。

import com.aliyuncs.utils.StringUtils;
import com.sms.service.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RestController
@CrossOrigin //跨域支持
public class SendSmsController {
   

    @Autowired
    private SendSms sendSms;

    private RedisTemplate<String,String> redisTemplate;

    @GetMapping("/send/{phone}")
    public String code(@PathVariable("phone") String phone){
   
            String code =
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值