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 =