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

1.配置redis依赖
因为框架帮们集成了大部分的依赖和它自动配置类的特点,我们只需要在maven中配置后就可以使用了,极大的挺高了我们开发的效率!
org.springframework.bootspring-boot-starter-data-redis
2.配置yaml文件
我这里使用的是本地redis服务,所以使用的是本地ip地址,即即可;
如果是虚拟机上配置的redis服务则只需要修改ip地址即可(如果配置了端口号或密码则也要重新配置)
redis:port: 6379host: localhost
3.创建配置类
【springboot集成redis模拟手机发送验证码进行验证】@Configurationpublic class RedisConfig {@Bean//绑定yaml配置@ConfigurationProperties(prefix = "redis")public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {//创建RedisTemplate模板RedisTemplate template = new RedisTemplate<>();//关联redisConnectionFactorytemplate.setConnectionFactory(redisConnectionFactory);//创建序列化类GenericToStringSerializer genericToStringSerializer = new GenericToStringSerializer(Object.class);//序列化类,对象映射设置//设置value的转化格式和key的转化格式template.setValueSerializer(genericToStringSerializer);template.setKeySerializer(new StringRedisSerializer());template.afterPropertiesSet();return template;}}
4.前端部分,一个简单的测试表单


//verifyCodevar 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 =http://www.kingceram.com/post/="true"){t = 120;clearInterval(interval);interval = setInterval("refer()",1000);//启动一秒定时}else if (data =http://www.kingceram.com/post/="limit"){clearInterval(interval);$("#countdown").text("单日发送超过次数");$("#sendCode").attr('disabled',true);}})})})$("#verifyCode").click(function (){$.post("/verifyCode",$("#code_form").serialize(),function (data){if (data =http://www.kingceram.com/post/="true"){$("#result").attr("color","green");$("#result").text("验证成功!");clearInterval(interval);$("countdown").text("");}else {$("#result").attr("color","red");$("#result").text("验证失败!");}})})
5.控制层
//注入redis模板类@Autowiredprivate RedisTemplate redisTemplate;@RequestMapping("/sendCode")@ResponseBodypublic String sendCode(String phone_no){//获取随机验证码String code = getCode(6);//拼接 验证码的 keyString codeKey = "verifyCode:" + phone_no + ":code";//拼接 发送次数的 keyString 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")@ResponseBodypublic String verifyCode(String phone_no,String verify_code){String codeKey = "verifyCode:" + phone_no + ":code";String countKey = "verifyCode:" + phone_no + ":count";Set 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