1. ホーム
  2. データベース
  3. レディス

Redisによる携帯電話認証コード配信の模倣例

2022-01-21 10:39:36

本記事では、携帯電話の認証コード送信を模倣したRedisの実装サンプルを紹介し、以下のように共有します。

フローチャート

I: jedis依存パッケージの追加

2:Redisサービスへの接続が成功するかどうかのテスト

// Create a Jedis object for connecting to the Redis service (on the server via redis-server you need to specify the configuration file: redis-server /etc/redis.conf)
Jedis jedis = new Jedis("192.168.119.128", 6379);
String value = jedis.ping();
System.out.println(value);
jedis.close();

III: CAPTCHAを生成するメソッドを書く

/**
     * Method for generating a captcha
     * @return code
     */
    public static String getCode() {
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 6; i++) {
            int num = random.nextInt(10);
            code += num;
        }
        System.out.println(code);
        return code;
    }

IV: CAPTCHAを送信するメソッドを書く

/**
     * The user clicks to generate a verification code 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 phone number, get the number of times the phone number sends the verification code
        String countKey = "VerifyCode" + phone + ":count";
        // Verify the key of the code, get the verification code of the phone number
        String codeKey = "VerifyCode" + phone + ":code";
 
        // Get the countKey to determine whether the current phone number can send the verification code
        String count = jedis.get(countKey);
        if (count == null) {
            jedis.setex(countKey, 24 * 60 * 60, "1");
        } else if (Integer.parseInt(count) <= 2) {
            jedis.incr(countKey);
        } else if (Integer.parseInt(count) > 2) {
            System.out.println("The number of times the current phone number sends the verification code exceeds the limit, please send the verification code again tomorrow");
            jedis.close();
        }
 
        String code = getCode();
        jedis.setex(codeKey, 120, code);
 
        jedis.close();
    }

V: チェックキャプチャメソッドの書き方

/**
     * User input phone number and verification code for verification
     * @param phone
     * @param code
     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("Verification successful! ");
        } else {
            System.out.println("Checksum failed! ");
        }
 
        jedis.close();
    }

これは、携帯電話の検証コードの実装例のRedisの模倣に関するこの記事の終わりです、より関連する携帯電話の検証コードのRedisの模倣は、コンテンツを送信するスクリプトの家の前の記事を検索してくださいまたは次の関連記事を閲覧し続けるあなたは、将来的にもっとスクリプトの家をサポートしていることを願っています!.