대가는 결과를 만든다

JWT(JSON Web Token) 본문

개발/Web관련

JWT(JSON Web Token)

yunzema 2019. 3. 25. 11:46
반응형

Intro

- JWT는 Claim 기반 토큰이다

- Claim Token : Claim (사용자 정보, 데이터 속성)을 담고 있는 토큰




구성 

 1. HEADER : {

  typ : "JWT"         //토큰의 타입 jwt

  alg : "HS256"      //해싱 알고리즘 HMAC or SHA256 or RSA, SIGNATURE에서 해당 알고리즘이 사용된다.

        }


  2. PAYLOAD : 토큰에서 사용할 정보 즉 Claim이 저장되어 있는 부분. Base64Url로 인코딩. JSON처럼 Key/Value 방식이며,                     총 세가지로 나뉨.


1) Registered Claim : 토큰정보 표현 위해 이미 정해진 데이터 종류

- iss : issuer 토큰 발급자

- sub : subject 토큰 제목

- aud : audience 토큰 대상자

- exp : expiration 토큰 만료 시간 (NumbericDate 형식)

- nbf : 토큰이 활성화 되는 날짜

- iat : issued at 토큰 발급된 시간

- jti : JWT의 고유 식별자. 중복방지, 1회용 Access Token에 사용


2) Public Claim : 서로 충돌되지 않는 이름 보유해야함, 예시) { "https://hexlant.com": true }


3) Private Claim : 실질적인 정보를 저장하기 위해 서버와 클라이언트가 서로 정의하여 사용하는 Claim 부분


  3. SIGNATURE :

 - HEADER와 PAYLOAD의 각 값을 BASE64로 인코딩,

 - 그 값을 비밀키를 이용해 헤더에서 정의한 알고리즘으로 해싱

 - 이 값을 다시 BASE64로 인코딩하여 생성


ex) HMACSHA256( base64UrlEncode(header) + "." +

base64UrlEncode(payload), secret)

  

예시)

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9

.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ

.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c


"."구분자로 나눠 표현되며 각 BASE64로 인코딩되어 있음.





동작원리



In authentication, when the user successfully logs in using his credentials, a JSON Web Token will be returned and must be saved locally (typically in local storage, but cookies can be also used), instead of the traditional approach of creating a session in the server and returning a cookie.

Whenever the user wants to access a protected route, it should send the JWT, typically in the Authorization header using the Bearer schema. Therefore the content of the header should look like the following.

Authorization: Bearer <token>

This is a stateless authentication mechanism as the user state is never saved in the server memory. The server’s protected routes will check for a valid JWT in the Authorization header, and if there is, the user will be allowed. As JWTs are self-contained, all the necessary information is there, reducing the need of going back and forward to the database.

This allows to fully rely on data APIs that are stateless and even make requests to downstream services. It doesn’t matter which domains are serving your APIs, as Cross-Origin Resource Sharing (CORS) won’t be an issue as it doesn’t use cookies.


참고 : https://auth0.com/learn/json-web-tokens/


비대칭키/대칭키, RSA 참고 : https://rsec.kr/?p=426



추가적으로 다른 설명 및 샘플 제작 링크 : https://medium.com/dev-bits/a-guide-for-adding-jwt-token-based-authentication-to-your-single-page-nodejs-applications-c403f7cf04f4


'개발 > Web관련' 카테고리의 다른 글

React, Vue Comparison 비교  (0) 2019.05.15
babel-polyfil  (0) 2019.04.15
DOM event 종류 Docs 참고  (0) 2019.01.28
bootstrap  (0) 2019.01.23
HTML에서 <canvas>란?  (0) 2019.01.21
Comments