Get the App
SLTechnology News&Howtos  ›  Development  › 

How Redis imitates the sending of mobile CAPTCHA

Shulou Source: shulou.com Published: 2022-06-02 19:40:34 10月05日 Update

This article will explain in detail how Redis imitates the sending of mobile CAPTCHA. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

The details are as follows:

Flow chart

One: add jedis dependency package

Second: test whether the connection to the Redis service is successful / / create a Jedis object to connect to the Redis service (specify a configuration file: redis-server / etc/redis.conf on the server via redis-server) Jedis jedis = new Jedis ("192.168.119.128", 6379); String value = jedis.ping (); System.out.println (value); jedis.close () Third, write the method of generating CAPTCHA / * generate CAPTCHA * @ return code * / public static String getCode () {Random random = new Random (); String code = ""; for (int I = 0; I < 6; iCAPTCHA +) {int num = random.nextInt (10); code + = num } System.out.println (code); return code;} IV: write the method of sending CAPTCHA / * users click to generate CAPTCHA and add it to redis * @ param phone * / public static void sendVerifyCode (String phone) {Jedis jedis = new Jedis ("192.168.119.128", 6379) / / key of the mobile phone number, obtain the number of times the mobile number sends CAPTCHA String countKey = "VerifyCode" + phone + ": count"; / / key of the CAPTCHA, obtain the CAPTCHA of the mobile number String codeKey = "VerifyCode" + phone + ": code"; / / obtain the countKey to determine whether the current mobile number can send the CAPTCHA String count = jedis.get (countKey) If (count = = null) {jedis.setex (countKey, 24 * 60 * 60, "1");} else if (Integer.parseInt (count) 2) {System.out.println ("the number of times the current mobile number sends CAPTCHA exceeds the limit, please send CAPTCHA tomorrow"); jedis.close ();} String code = getCode () Jedis.setex (codeKey, 120, code); jedis.close ();} V: write verification code method / * user input mobile phone number and verification code for verification * @ param phone * @ param code * / public static void CustomerVerifyCode (String phone, String code) {Jedis jedis = new Jedis ("192.168.119.128", 6379) String codeKey = "VerifyCode" + phone + ": code"; String phoneVerifyCode = jedis.get (codeKey); if (phoneVerifyCode.equals (code)) {System.out.println ("check successful!") ;} else {System.out.println ("Verification failed!") ;} jedis.close ();} this is the end of the article on "how Redis imitates the sending of mobile CAPTCHA". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Authentication mobile phone number mobile number method article service generation success mobile number more times user good practical upper limit content object file article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Apple Linux Shulou Tech Info NVidia