irpas技术客

springboot集成redis模拟手机发送验证码进行验证_weixin_49862197_redis springboot 短信验证码

大大的周 3362

1.配置redis依赖

因为springboot框架帮们集成了大部分的依赖和它自动配置类的特点,我们只需要在maven中配置后就可以使用了,极大的挺高了我们开发的效率!

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

2.配置yaml文件

我这里使用的是本地redis服务,所以使用的是本地ip地址,即localhost即可;

如果是虚拟机上配置的redis服务则只需要修改ip地址即可(如果配置了端口号或密码则也要重新配置)

redis: port: 6379 host: localhost

3.创建redisConfig配置类

@Configuration public class RedisConfig { @Bean //绑定yaml配置 @ConfigurationProperties(prefix = "redis") public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { //创建RedisTemplate模板 RedisTemplate<String, Object> template = new RedisTemplate<>(); //关联redisConnectionFactory template.setConnectionFactory(redisConnectionFactory); //创建序列化类 GenericToStringSerializer genericToStringSerializer = new GenericToStringSerializer(Object.class); //序列化类,对象映射设置 //设置value的转化格式和key的转化格式 template.setValueSerializer(genericToStringSerializer); template.setKeySerializer(new StringRedisSerializer()); template.afterPropertiesSet(); return template; } }

4.前端部分,一个简单的测试表单

<form id="code_form" class="navbar-expand-sm"> <input type="text" placeholder="请输入手机号" th:name="phone_no"> <button type="button" id="sendCode">发送验证码</button><br> <font id="countdown" color="red"></font> <br><input type="text" placeholder="请填写验证码" th:name="verify_code"> <button type="button" id="verifyCode">确定</button><br> <font id="result" color="green"></font> <font id="error" color="red"></font> </form> <script type="text/javascript"> //verifyCode var t = 120;//设置倒计时时间 var interval; function refer(){ $("#countdown").text("请于"+t+"秒内填写验证码");//显示倒计时 t--;//计时器递减 if(t<=0){ clearInterval(interval); $("#countdown").text("验证码已失效请重新发送!"); } } $(function (){ $("#sendCode").click(function (){ $.post("/sendCode",$("#code_form").serialize(),function (data){ if (data == "true"){ t = 120; clearInterval(interval); interval = setInterval("refer()",1000);//启动一秒定时 }else if (data == "limit"){ clearInterval(interval); $("#countdown").text("单日发送超过次数"); $("#sendCode").attr('disabled',true); } }) }) }) $("#verifyCode").click(function (){ $.post("/verifyCode",$("#code_form").serialize(),function (data){ if (data == "true"){ $("#result").attr("color","green"); $("#result").text("验证成功!"); clearInterval(interval); $("countdown").text(""); }else { $("#result").attr("color","red"); $("#result").text("验证失败!"); } }) })

5.控制层

//注入redis模板类 @Autowired private RedisTemplate redisTemplate; @RequestMapping("/sendCode") @ResponseBody public String sendCode(String phone_no){ //获取随机验证码 String code = getCode(6); //拼接 验证码的 key String codeKey = "verifyCode:" + phone_no + ":code"; //拼接 发送次数的 key String countKey = "verifyCode" + phone_no + ":count"; String count = (String) redisTemplate.boundValueOps(countKey).get(); //获取key的自增后的值 RedisAtomicLong redisAtomicLong = new RedisAtomicLong(countKey, redisTemplate.getConnectionFactory()); long countLong = redisAtomicLong.incrementAndGet(); System.out.println(countLong); //判断时候是第一次 if (countLong == 0) { redisTemplate.boundValueOps(countKey).set(1); }else if (countLong >3){ return "limit"; } //向redis中存储,以手机号为键 redisTemplate.boundSetOps(codeKey).add(code); System.out.println(redisTemplate.boundSetOps(codeKey).members()); //设置过期时间 redisTemplate.expire(codeKey,2, TimeUnit.MINUTES); return "true"; } @RequestMapping("/verifyCode") @ResponseBody public String verifyCode(String phone_no,String verify_code){ String codeKey = "verifyCode:" + phone_no + ":code"; String countKey = "verifyCode:" + phone_no + ":count"; Set<String> members = redisTemplate.boundSetOps(codeKey).members(); //判断存储的验证码是否包含输入的验证码 if (members.contains(verify_code)){ return "true"; } return "false"; } //设置一个随机生成验证码的工具类 public String getCode(int length){ String code = "" ; Random random = new Random(); for (int i = 0; i < length; i++) { int rand = random.nextInt(10); code += rand; } return code; }

6.思路

输入一串号码发验证码,因为设置的120秒;当超过120秒验证码就会失效;其实就是设置key的过期时间。

当我们点击发送验证码时,后台接收到了前端传过来的手机号并将其作为key存入redis中,再将随机生成的验证码作为key的值存入redis;

此时redis里就存在这个key和value值了,然后再判断key的value值是否和前端输入来的验证码是否相同就好了;


同时后台显示的是发送次数和验证码


在120秒内输入正确的验证码

? ? ? ? ? ? ? ? ? ? ? ?

设置一天发送的次数,设置key的value为自增+1即可;

客户端每一次向后台发送验证码时就将记录次数的key的value值+1就好了,然后再获取自增后的值进行判断即可

?


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #redis #springboot #短信验证码 #redis模拟实现手机验证