说明

  1. 只是模拟给手机发送短信验证码,并且进行校验
  2. 真正能够给手机发送验证码短信还需要结合第三方,如阿里云、讯飞等

需求

  • 发送手机号,返回验证码(6位随机数字)
    • 每个手机号一天只能发送10次(为了测试方便,可以改成3次),每天0点重置次数
    • 当天获取次数达到上限,提示“当天验证码获取次数已达上限,请明天再次使用”
    • 每个验证码有效期为5分钟,获取验证码提示“【XX系统】您的手机验证码为:XXXXXX,有效期5分钟,此功能每天最多获取XX条,请勿向任何人出示,以免账号被盗”
    • 5分钟内再次获取验证码提示“已有可用验证码,XX分XX秒内依然有效”
  • 发送手机号和验证码,对手机号和验证码进行校验,返回验证结果
    • 提示:“验证通过”或“验证失败,请确认手机号或验证码”

代码实现

1.依赖

		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

2.Controller

@RestController
public class PhoneController {
    @Resource
    private PhoneService phoneService;

    @RequestMapping("/send")
    public String send(String phone) {
        return phoneService.send(phone);
    }

    @RequestMapping("/check")
    public String check(String phone,String code) {
        return phoneService.check(phone,code);
    }
}

3.service

3.1常量设定

    private static final int MAX_TIME_OUT = 5;
    private static final String MAX_PHONE_COUNT= "3";
    private static final String PHONE_COUNT_SUFFIX=":COUNT";
    private static final String PHONE_CODE_SUFFIX=":CODE";

3.2判断获取次数

        if(RedisStringUtil.get(codeKey)==null &&
                "3".equals(RedisStringUtil.get(countKey))){
            return "当天验证码获取次数已达上限,请明天再次使用";
        }

3.3判断验证码剩余时间

        long exTime = RedisKeyUtil.getExpire(codeKey);
        if(exTime>0){
            return "已有可用验证码," + (exTime / 60) + "分 " + (exTime % 60) +
                    " 秒内依然有效";
        }

3.4获取验证码

		String.valueOf(new Random().nextInt(900000) + 100000);

3.5保存验证码,设置有效期,累加获取次数

        String code = getCode();
        RedisStringUtil.setEx(codeKey,code,MAX_TIME_OUT, TimeUnit.MINUTES);
        RedisStringUtil.incrBy(countKey,1);

3.6校验手机号与验证码

        String codeKey = phone + PHONE_CODE_SUFFIX;
        if(code!=null && code.equals(RedisStringUtil.get(codeKey))){
            return "验证成功";
        }
        return "验证失败,请确认手机号或验证码";

service层完整代码

@Service
public class PhoneService {
    private static final int MAX_TIME_OUT = 5;
    private static final String MAX_PHONE_COUNT= "3";
    private static final String PHONE_COUNT_SUFFIX=":COUNT";
    private static final String PHONE_CODE_SUFFIX=":CODE";
    public String send(String phone) {
        String countKey = phone + PHONE_COUNT_SUFFIX; //次数
        String codeKey = phone + PHONE_CODE_SUFFIX; //验证码
        if(RedisStringUtil.get(codeKey)==null &&
                "3".equals(RedisStringUtil.get(countKey))){
            return "当天验证码获取次数已达上限,请明天再次使用";
        }
        long exTime = RedisKeyUtil.getExpire(codeKey);
        if(exTime>0){
            return "已有可用验证码," + (exTime / 60) + "分 " + (exTime % 60) +
                    " 秒内依然有效";
        }
        String code = getCode();
        RedisStringUtil.setEx(codeKey,code,MAX_TIME_OUT, TimeUnit.MINUTES);
        RedisStringUtil.incrBy(countKey,1);
        return "【地笼系统】您的手机验证码为:" + code
                + ",有效期5分钟,此功能每天最多获取" + MAX_PHONE_COUNT +
                "条,请勿向任何人出示,以免账号被盗";
    }

    public String check(String phone, String code) {
        String codeKey = phone + PHONE_CODE_SUFFIX;
        if(code!=null && code.equals(RedisStringUtil.get(codeKey))){
            return "验证成功";
        }
        return "验证失败,请确认手机号或验证码";
    }

    private String getCode(){
        return String.valueOf(new Random().nextInt(900000) + 100000);
    }
}

4.设置定时任务,每天0点清除所有短信获取次数

@Configuration
public class TaskConfig {
    @Scheduled(cron = "10 18 * * * ?")
    public void clearPhoneCount(){
        System.out.println("-----------------");
        RedisKeyUtil.delete(RedisKeyUtil.keys("*:COUNT"));
    }
}

5.启动类上添加开启定时任务

@EnableScheduling
public class RedisdemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(RedisdemoApplication.class, args);
    }
}
04-30 15:14