보안 인증 (JWT 인증)

    보안 인증 (JWT 인증)


    The content is currently unavailable in English. You are viewing the default Korean version.
    Article summary

    JWT (JSON Web Tokens)는 암호화를 사용하여 데이터를 만드는 인터넷 표준 중의 하나입니다. JWT에 대한 보다 자세한 내용은 아래의 페이지를 통해 확인하시기 바랍니다.


    JWT 인증

    Secret Key를 이용하여 고객사의 서버에서 JWT 문자열을 만들어 인증하는 방식입니다. 대부분의 상황에서 권장하는 방식입니다.

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


    인증 토큰 생성 가이드

    Shoplive에서 사용하는 JWT payload는 다음과 같습니다.

    이 JWT payload는 라이브 플러그인과 숏폼이 공통으로 사용합니다. 이 중 gender, age, custom, userScore, userType, profile 필드는 라이브에서만 사용되며 숏폼에서는 사용되지 않습니다. 다만 같은 페이지에서 두 SDK를 함께 사용하는 경우, 나중에 호출되는 쪽이 이전 인증 정보를 완전히 대체하므로 이 필드들도 함께 포함하지 않으면 라이브 쪽 정보가 유실될 수 있습니다.

    Property name

    Description

    Sample

    Comment

    expiration

    (Required)

    만료 시간 (timestamp)

    1516239022

    토큰 만료 시간

    userId

    (Required)

    사용자 아이디

    sample_user_id

    로그인 사용자의 유일성을 보장할 수 있는 값. 값이 없으면 별도 안내 없이 로그인 상태가 적용되지 않습니다.

    name

    사용자 이름

    (댓글에서 사용)

    닉네임

    댓글 작성 시 표시되는 이름입니다.

    gender

    사용자 성별

    빈 값(미지정), m(남), f(여)

    라이브에서만 사용

    age

    사용자 나이

    25

    라이브에서만 사용

    custom

    사용자 임의 데이터

    custom data

    최대 1KB까지 설정 가능. 라이브에서만 사용

    userScore

    사용자 등급

    10

    -100~100 정수. 라이브에서만 사용

    userType

    사용자 유형

    VIP

    라이브에서만 사용

    profile

    사용자 프로필 이미지 URL

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

    권장 이미지 크기 64x64px. 라이브에서만 사용


    샘플 코드 (JWT 생성)

    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) {
            /*
             * 암호화 알고리즘
             * 지원하는 알고리즘은 https://jwt.io/ 혹은 https://github.com/jwtk/jjwt 사이트를 참조하세요
             */
            String algorithm = "HS256";
            /*
             * 발급 받은 base64 인코딩된 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();
            /*
             * 사용자 아이디
             * 이 아이디를 기준으로 서비스 사용자를 구분하며 최대 255바이트까지 설정할 수 있습니다
             */
            String userId ="sample_user_id";
            /*
             * 댓글에 노출할 사용자명
             */
            String name = "my_nickname";
            /*
             * 아래 필드는 숏폼에서는 사용하지 않고 라이브에서만 사용합니다.
             * 라이브 플러그인과 같은 페이지에서 함께 쓰는 경우, 정보 유실 방지를 위해 함께 전달해 주세요.
             */
            String gender = "f";
            Integer age = 15;
            String custom = "{Custom Data}";
            int userScore = 34;
            String userType = "VIP";
            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("userType", userType)
                .claim("profile", profile);
    
            String jwt = builder.compact();
            System.out.printf("jwt: " + jwt);
        }
    }
    
    Result
    
    jwt: eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MTA1NzA1MDgsImlhdCI6MTYxMDUyNzMwOCwidXNlcklkIjoic2FtcGxlX3VzZXJfaWQiLCJuYW1lIjoibXlfbmlja25hbWUiLCJnZW5kZXIiOiJmIiwiYWdlIjoxNSwiY3VzdG9tIjoie0N1c3RvbSBEYXRhfSIsInVzZXJTY29yZSI6MzQsInVzZXJUeXBlIjoiVklQIiwicHJvZmlsZSI6Imh0dHBzOi8vaW1hZ2Uuc2hvcGxpdmUuY2xvdWQvc2FtcGxlX3Byb2ZpbGUucG5nIn0.-82lcDrrwJZF4AkeRthegHryEup_B34wkFfQo2YJvfI
    

    JWT를 생성하고 Java 이외의 프로그래밍 언어를 통해 인증을 실행하려면 SHOPLIVE 담당자에게 지원을 요청하세요.