[HTTP/Network] Token

Hannahhhยท2022๋…„ 9์›” 14์ผ
0

HTTP/Network

๋ชฉ๋ก ๋ณด๊ธฐ
11/12

๐Ÿ” Token


์„ธ์…˜ ๊ธฐ๋ฐ˜ ์ธ์ฆ์€ ์„œ๋ฒ„(or DB)์— ์œ ์ € ์ •๋ณด๋ฅผ ๋‹ด๋Š” ๋ฐฉ์‹์œผ๋กœ, ๋งค ์š”์ฒญ๋งˆ๋‹ค DB๋ฅผ ์‚ดํŽด๋ณด๋Š” ๊ฒƒ์ด ๋ถˆํŽธํ•˜๊ณ  ์„œ๋ฒ„์— ๋ถ€๋‹ด์„ ์ค€๋‹ค.
๋”ฐ๋ผ์„œ ์„œ๋ฒ„ ๋ถ€๋‹ด์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ, ๋Œ€ํ‘œ์ ์ธ ํ† ํฐ ๊ธฐ๋ฐ˜ ์ธ์ฆ์€ JWT(JSON Web Token)์ด ์žˆ๋‹ค.

ํ† ํฐ์€ ์•”ํ˜ธํ™”๋œ ์ƒํƒœ์˜ ์œ ์ € ์ •๋ณด๋ฅผ ๊ฐ€์ง€๋ฉฐ, ์•”ํ˜ธํ™”ํ–ˆ๊ธฐ๋•Œ๋ฌธ์— ํด๋ผ์ด์–ธํŠธ์— ๋‹ด์„ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ, ํด๋ผ์ด์–ธํŠธ์—์„œ ์ธ์ฆ ์ •๋ณด๋ฅผ ๋ณด๊ด€ํ•œ๋‹ค.



๐Ÿ‘€ JWT


Json Web Token์˜ ์•ฝ์ž๋กœ, Json ํฌ๋งท์œผ๋กœ ์‚ฌ์šฉ์ž์— ๋Œ€ํ•œ ์†์„ฑ์„ ์ €์žฅํ•˜๋Š” ์›น ํ† ํฐ์„ ์˜๋ฏธํ•œ๋‹ค.

์—‘์„ธ์Šค ํ† ํฐ(Access Token) ๊ณผ ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ(Refresh Token) ์„ ํ•„์š”์— ๋งž๊ฒŒ ์ด์šฉํ•ด ์ธ์ฆ์„ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ, ๊ถŒํ•œ์„ ๋ถ€์—ฌ ๋ฐ›๋Š”๋ฐ์—๋Š” ์—‘์„ธ์Šค ํ† ํฐ๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด ๋˜๊ณ , ์ •๋ณด๋ฅผ ์ง€ํ‚ค๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•œ ์›น์‚ฌ์ดํŠธ์˜ ๊ฒฝ์šฐ, ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

๊ถŒํ•œ ๋ถ€์—ฌ์— ๊ต‰์žฅํžˆ ์œ ์šฉํ•˜๋‹ค.
์˜ˆ๋ฅผ๋“ค์–ด A์•ฑ์—์„œ Gmail๊ณผ ์—ฐ๋™๋˜์–ด ์ด๋ฉ”์ผ์„ ์ฝ์–ด์™€์•ผํ•œ๋‹ค๋ฉด, ์œ ์ €๊ฐ€ Gmail ์ธ์ฆ ์„œ๋ฒ„์— ๋กœ๊ทธ์ธ ์ •๋ณด๋ฅผ ์ œ๊ณตํ–ˆ์„ ๋•Œ, JWT๋ฅผ ๋ฐœ๊ธ‰๋ฐ›๊ณ , A์•ฑ์€ JWT๋ฅผ ์‚ฌ์šฉํ•ด ์œ ์ €์˜ Gmail ์ด๋ฉ”์ผ์„ ์ฝ์„ ์ˆ˜ ์žˆ๊ฒŒ๋˜๋Š” ๊ฒƒ์ด๋‹ค.



โœ” JWT ๊ตฌ์กฐ


์„ธ ๋ถ€๋ถ„ ๋ชจ๋‘ base64 ๋ฐฉ์‹์œผ๋กœ ์ธ์ฝ”๋”ฉ๋œ๋‹ค.


  • Header

    ์–ด๋–ค ์ข…๋ฅ˜์˜ ํ† ํฐ์ธ์ง€, ์–ด๋–ค ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์‹œ๊ทธ๋‹ˆ์ฒ˜๋ฅผ ์•”ํ˜ธํ™”(sign)ํ•  ๊ฒƒ์ธ์ง€ JSON ํ˜•ํƒœ๋กœ ๋ช…์‹œ๋˜์–ด ์žˆ๋‹ค.

  • Payload

    ์„œ๋ฒ„์—์„œ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ €์˜ ์ •๋ณด๊ฐ€ ๋‹ด๊ฒจ ์žˆ๋‹ค. ์‹œ๊ทธ๋‹ˆ์ฒ˜๋ฅผ ํ†ตํ•ด ์œ ํšจ์„ฑ ๊ฒ€์ฆ์ด ๋˜์ง€๋งŒ, ๋””์ฝ”๋”ฉ์ด ์‰ฝ๊ธฐ ๋•Œ๋ฌธ์— ๋ฏผ๊ฐํ•œ ์ •๋ณด๋Š” ๋‹ด์ง€ ์•Š๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

  • Signature

    ์„œ๋ฒ„์˜ ๋น„๋ฐ€ ํ‚ค์™€ ํ—ค๋”์—์„œ ์ง€์ •ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•˜์—ฌ ์•”ํ˜ธํ™”ํ•œ๋‹ค.
    ์•ž์˜ Header์™€ payload์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ๋น„๋ฐ€ํ‚ค๋ฅผ ์‚ฌ์šฉํ•ด ์•”ํ˜ธํ™”ํ•œ ๊ฐ’์ด๋ฏ€๋กœ, ๋ณด์•ˆ์„ฑ์ด ๋†’๋‹ค.




๐Ÿ‘€ ํ† ํฐ ๊ธฐ๋ฐ˜ ์ธ์ฆ ๊ณผ์ •


1. Client -> Server : ID/password๋ฅผ ๋‹ด์•„ ๋กœ๊ทธ์ธ ์š”์ฒญ

2. Server : ID/password ํ™•์ธ ํ›„, Client์— ๋ณด๋‚ผ ์•”ํ˜ธํ™”๋œ ํ† ํฐ ์ƒ์„ฑ(access, refresh ๋ชจ๋‘ ์ƒ์„ฑ)

3. Server -> Client : ํ† ํฐ ์ „์†ก

4. Client : ํ† ํฐ ์ €์žฅ(in Local Storage, Session Storage, Cookie ๋“ฑ)

5. Client -> Server : HTTP Header(Authentication Header) ๋˜๋Š” ์ฟ ํ‚ค์— ํ† ํฐ์„ ๋‹ด์•„์„œ ์ „์†ก(ex. Cookie์—๋Š” refresh ํ† ํฐ, body์—๋Š” access ํ† ํฐ ๋‹ด์•„์„œ ์ „์†ก), Authentication Header ์‚ฌ์šฉ ์‹œ Bearer Authentication์„ ์ด์šฉ.

6. Server : ํ† ํฐ ํ•ด๋…, ๋ฐœ๊ธ‰ํ•ด์ค€ ํ† ํฐ์ด ๋งž๋‹ค๋ฉด Client์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌ ํ›„ ์‘๋‹ต ์ „์†ก



โœ” ์žฅ์ 


  • ๋ฌด์ƒํƒœ์„ฑ & ํ™•์žฅ์„ฑ(Statelessness & Scalability)
    ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•  ํ•„์š”๊ฐ€ ์—†๊ณ , ํด๋ผ์ด์–ธํŠธ๋Š” ์š”์ฒญ์„ ๋ณด๋‚ผ๋•Œ ๋งˆ๋‹ค ํ† ํฐ์„ ํ—ค๋”์— ํฌํ•จ์‹œํ‚ค๋ฉด ๋œ๋‹ค.

  • ์•”ํ˜ธํ™”ํ•œ ํ† ํฐ์„ ์‚ฌ์šฉํ•˜๊ณ , ์•”ํ˜ธํ™” ํ‚ค๋ฅผ ๋…ธ์ถœํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ๋•Œ๋ฌธ์— ์•ˆ์ „ํ•˜๋‹ค.

  • ์–ด๋Š ์„œ๋ฒ„์—์„œ๋‚˜ ์ƒ์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

  • ๊ถŒํ•œ ๋ถ€์—ฌ์— ์œ ์šฉํ•˜๋‹ค.
    payload ์•ˆ์— ์–ด๋–ค ์ •๋ณด์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ์ง€ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.




Reference: ์ฝ”๋“œ์Šคํ…Œ์ด์ธ 
https://hackernoon.com/using-session-cookies-vs-jwt-for-authentication-sd2v3vci

0๊ฐœ์˜ ๋Œ“๊ธ€