ユーザー認証 - JWT認証

    ユーザー認証 - JWT認証


    記事の要約

    導入

    JWT(JSON Web Tokens)は、暗号化データを作成するためのインターネット技術の1つです。JWTの詳細については、以下のページを参照してください。

    Advanced authentication (JWT authentication)

    JWT での認証

    秘密鍵を使用して顧客のサーバー上でJWT文字列を作成および認証する方法です。これは、ほとんどの状況で推奨される方法です。

    <script type="text/javascript" src="https://static.shoplive.cloud/shoplive.js"></script>
    <script>cloud.shoplive.init({ accessKey: 'YOUR ACCESS KEY HERE', userJWT: "USER_JWT" });</script>
    
    <!-- OR -->
    
    <script type="text/javascript" src="https://static.shoplive.cloud/shoplive.js"></script>
    <script>
      cloud.shoplive.init({ accessKey: 'YOUR ACCESS KEY HERE' });
      cloud.shoplive.setUserJWT("USER_JWT");
    </script>

    認証トークン生成のガイド

    Shoplive で使用される JWT ペイロードは次のとおりです。

    プロパティ

    説明

    サンプル

    コメント

    expiration
    (必須)

    有効期限 (タイムスタンプ)

    1516239022

    トークンの有効期限

    userId
    (必須)

    ユーザーID

    sample_user_id

    ログインしたユーザーの一意性を保証する値

    name

    ユーザー名
    (チャットウィンドウで使用)

    my_nickname

    名前を指定しない場合は、チャットでユーザー入力を強制するか、キャンペーンのチャット設定に従ってランダムに作成することができます。

    gender

    ユーザーの性別

    空 (指定なし)、m (男性)、f (女性)

    合計/ログイン視聴者のデータを性別ごとに確認できます。

    age

    ユーザーの年齢

    25

    合計/ログイン視聴者のデータを年齢ごとに確認できます。

    custom

    カスタムデータ

    カスタムデータ

    任意のデータを1KBまで設定できます。
    合計/ログイン視聴者のデータをカスタムデータの区分ごとに確認できます。

    userScore

    ユーザー評価

    ユーザーレーティングを設定すると、レーティングに応じて入場イベントや抽選イベントをパーソナライズするための基準として使用できます。
    整数は -100 から 100 まで使用できます。

    ユーザーレーティングを設定すると、レーティングに応じて入場イベントや抽選イベントをパーソナライズするための基準として使用できます。
    整数は -100 から 100 まで使用できます。

    profile

    ユーザー プロフィール画像の URL

    https://image.shoplive.cloud/sample_profile.png

    ライブ放送のチャット一覧にプロフィール画像を表示する場合、画像のパスを直接設定できます。
    推奨される画像サイズは 64px X 64px です。画像ファイルのサイズを過度に大きくしないようご注意ください。


    Java

    import io.jsonwebtoken.JwtBuilder;
    import io.jsonwebtoken.Jwts;
    import io.jsonwebtoken.SignatureAlgorithm;
    import java.util.Date;
    public class JwtAuthorizationGeneratorSample {
        public static void main(String[] args) {
            /*
             * Encryption algorithm
             * Refer to https://jwt.io or https://github.com/jwtk/jjwt for supported algorithms
             */
            String algorithm = "HS256";
            /*
             * The value issued from SHOPLIVE base64 encoded secret key
             */
            String base64EncodedSecretKey = "ckFXaWtRWENtSTA2QnpGVmxWNlBySWF4cUk1Q1pxbHU=";
            /*
             * Set expiring time for JWT token as 12 hours
             */
            long expiration = System.currentTimeMillis() + 12 * (60 * 60 * 1000);
            // OR
            // long expiration = LocalDateTime.now().plus(12, ChronoUnit.HOURS).atZone(ZoneId.systemDefault()).toEpochSecond();
            /*
             * User ID
             * User ID is the value that can identify the viewers uniquely and can be set up to 255 bytes.
             */
            String userId = "sample_user_id";
            /*
             * Nickname 
             * Banned words are replaced with '*'.
             */
            String name = "my_nickname";
            /*
             * Additional information for analyzing users based on the gender in the data insights.
             * male(m), female(f) others(n, null, etc.)
             */
            String gender = "f";
            /*
            /* Age or generation. Additional information for analyzing users based on age/generation in the data insights.
             * You can enter any non-negative number and will be summarized in the data insights grouped by 5 years.
             */
            Integer age = 15;
            
            /**
             * Provide the custom data along with the user information when downloading the list of participants from the data insights for storing user information in addition to userId.
             */
            String custom = "{\"userGrade\":\"VIP\"}";
            
            /**
             * Set user score. You can utilize these values when selecting winners for events or quizzes.
             */
            int userScore = 34;
          
            /**
             * User profile image URL
             */
            String profile = "https://image.shoplive.cloud/sample_profile.png";
    
            JwtBuilder builder = Jwts.builder()
                .signWith(SignatureAlgorithm.forName(algorithm), base64EncodedSecretKey)
                .setExpiration(new Date(expiration))
                .setIssuedAt(new Date())
                .claim("userId", userId)
                .claim("name", name)
                .claim("gender", gender)
                .claim("age", age)
                .claim("custom", custom)
                .claim("userScore", userScore)
                .claim("profile", profile);
          
            String jwt = builder.compact();
            System.out.printf("jwt: " + jwt);
        }
    }
    Result
    
    jwt: eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTA1NzA1MDgsImlhdCI6MTYxMDUyNzMwOCwidXNlcklkIjoic2FtcGxlX3VzZXJfaWQiLCJuYW1lIjoibXlfbmlja25hbWUiLCJnZW5kZXIiOiJmIiwiYWdlIjoxNX0.0Z5YUo99149fIFyfqwxa-7SLyqC0RHR1T8P7jZLhpG8
    

    📘

    JWTを生成し、Java以外のプログラミング言語で認証を実行する場合は、SHOPLIVE担当者に連絡してサポートを受けてください。