In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "Redis how to send CAPTCHA and limit the number of times per day". The content in the article is simple and clear, and it is easy to learn and understand. Please follow Xiaobian's train of thought to study and learn "how to send CAPTCHA and limit the number of times per day by Redis".
1. Function
Enter the mobile phone number, click send and randomly generate a six-digit code, which is valid for 2 minutes.
Enter the CAPTCHA, click verify, and return success or failure
Each mobile phone number can only be lost three times a day.
2. Analysis
Each phone can only be input 3 times a day: after each incr is sent, + 1 indicates that it cannot be sent when the value is 3, and the expiration time is the end of the day.
Randomly generate 6-digit CAPTCHA: RandomUtil (hutool)
The verification code is valid for 2 minutes: put it in redis and set the expiration time for 2 minutes.
Determine whether the CAPTCHA is consistent: obtain the CAPTCHA from redis and compare it with the entered CAPTCHA
3. Implement package cn.ken.blog.controller.common;import cn.hutool.core.date.DateUnit;import cn.hutool.core.date.DateUtil;import cn.hutool.core.util.RandomUtil;import cn.ken.blog.common.constant.Constants;import cn.ken.blog.common.domain.Result;import cn.ken.blog.common.enums.ErrorCodeEnum;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.scheduling.annotation.Scheduled Import org.springframework.util.ObjectUtils;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;import java.util.Date;import java.util.concurrent.TimeUnit;/** * CAPTCHA controller * @ author Ken-Chy129 * @ date 2022-4-17 20:28 * / @ RestController@SuppressWarnings (value = {"unchecked", "rawtypes"}) public class CaptureController {@ Autowired private RedisTemplate redisTemplate / / generate the verification code @ GetMapping ("getNumCode") public Result getNumCode (String phone) {String captureLimitKey = Constants.CAPTCHA_LIMIT_KEY + phone; Integer counts = (Integer) redisTemplate.opsForValue () .get (captureLimitKey) If (ObjectUtils.isEmpty (counts)) {/ / the first verification today, so how many milliseconds left between long expire = DateUtil.endOfDay (new Date ()) .between (new Date (), DateUnit.MS); redisTemplate.opsForValue () .set (captureLimitKey, 1, expire, TimeUnit.MILLISECONDS) } else if (counts < 3) {/ / does not exceed the limit number of times redisTemplate.opsForValue () .increment (captureLimitKey);} else {/ / exceeds the limit number, does not generate a CAPTCHA, and directly returns return new Result () .error (ErrorCodeEnum.OVER_LIMITS) } / / generate verification code String code = RandomUtil.randomNumbers (6); / / randomly generate six digits String captureCodeKey = Constants.CAPTCHA_CODE_KEY + phone; redisTemplate.opsForValue (). Set (captureCodeKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); return new Result (). Success (captureCodeKey + ":" + code) } / / Verification code @ GetMapping ("verify") public Result verify (String phone, String code) {String captureCodeKey = Constants.CAPTCHA_CODE_KEY + phone; String realCode = (String) redisTemplate.opsForValue () .get (captureCodeKey) The verification code generated by this user does not exist in if (ObjectUtils.isEmpty (realCode)) {/ / redis. It is proved that the verification code is expired to destroy return new Result (). Error (ErrorCodeEnum.OVERDUE_CODE);} if (realCode.equals (code)) {return new Result (). Success ("verification successful") } else {return new Result (). Error (ErrorCodeEnum.ERROR_CODE);}} / / @ Scheduled (cron = "0 012 * *?) / / private void clear () {/ / redisTemplate.delete () / /} / Constants class / * CAPTCHA redis key * / public static final String CAPTCHA_CODE_KEY =" captcha_codes: " / * * Daily limit redis key * / public static final String CAPTCHA_LIMIT_KEY = "captcha_limits:"; / * validity period of CAPTCHA (minutes) * / public static final Integer CAPTCHA_EXPIRATION = 2 Thank you for your reading, the above is the content of "Redis how to send CAPTCHA and limit the number of times per day". After the study of this article, I believe you have a deeper understanding of how Redis can send CAPTCHA and limit the number of times per day, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.