# [Security] JWT(Json Web Token) ์ธ์ฆ ๋ฐฉ์‹
Study Repository

[Security] JWT(Json Web Token) ์ธ์ฆ ๋ฐฉ์‹

by rlaehddnd0422

JWT(JSON Web Token)์€ ์ธ์ฆ์— ํ•„์š”ํ•œ ์ •๋ณด๋“ค์„ ์•”ํ˜ธํ™” ์‹œํ‚จ JSON ํ† ํฐ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

JWT ๊ธฐ๋ฐ˜ ์ธ์ฆ ๋ฐฉ์‹์€ JWT์„ HTTP ํ—ค๋”์— ์‹ค์–ด ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์‹๋ณ„ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.


โ—๏ธJWT(Json Web Token)  

ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌํ•  JWT = Base64 Encode(Header + payload + Signature)

 

JWT์—๋Š” ๊ฐ๊ฐ์˜ ๊ตฌ์„ฑ์š”์†Œ๊ฐ€ ์ (.)์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์žˆ์œผ๋ฉฐ ๊ตฌ์„ฑ์š”์†Œ๋Š” 3๊ฐ€์ง€์ž…๋‹ˆ๋‹ค.

 

Header : ํ† ํฐ์˜ ํƒ€์ž…, JWT ์ƒ์„ฑ์— ์‚ฌ์šฉ๋  ํ•ด์‰ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

Payload : ํ† ํฐ์—์„œ ์‚ฌ์šฉํ•  ์ •๋ณด์˜ ์กฐ๊ฐ๋“ค์ธ Claim ์ด ๋‹ด๊ฒจ์žˆ์Šต๋‹ˆ๋‹ค. (์‹ค์ œ JWT ๋ฅผ ํ†ตํ•ด์„œ ์•Œ ์ˆ˜ ์žˆ๋Š” ๋ฐ์ดํ„ฐ)

์ฆ‰, ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ฃผ๊ณ ๋ฐ›๋Š” ์‹œ์Šคํ…œ์—์„œ ์‹ค์ œ๋กœ ์‚ฌ์šฉ๋  ์ •๋ณด์— ๋Œ€ํ•œ ๋‚ด์šฉ์„ ๋‹ด๊ณ  ์žˆ๋Š” ์„น์…˜์ž…๋‹ˆ๋‹ค.

 

Signature : ์‹œ๊ทธ๋‹ˆ์ฒ˜๋Š” "์ •์˜ํ•œ Header์™€ Palyoad๋ฅผ Base64๋กœ ์ธ์ฝ”๋”ฉํ•œ ๊ฐ’ + ์„œ๋ฒ„์˜ Secret Key(Base64 ์ธ์ฝ”๋”ฉ์€ ์„ ํƒ)"์„ Header์—์„œ ์ •์˜ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ•ด์‹œํ™”ํ•œ ๊ฐ’์ž…๋‹ˆ๋‹ค.

โžก๏ธ Signature : HS_Algorithm(Base64Encode(Header) + Base64Encode(Payload) + Secret Key ) 

 

 

โ–ถ๏ธŽ JWT = Base64 Encode(Header + payload + Signature(HS_Algorithm(Base64Encode(Header) + Base64Encode(Payload) + Server's Secret Key))

 

๊ฐ€์žฅ ์ค‘์š”ํ•œ๊ฑด ์„œ๋ฒ„์˜ ์‹œํฌ๋ฆฟ ํ‚ค!

Header์™€ Payload๋Š” ๋‹จ์ˆœํžˆ ์ธ์ฝ”๋”ฉ๋œ ๊ฐ’์ด๊ธฐ ๋•Œ๋ฌธ์— ์ œ 3์ž๊ฐ€ Decodeํ•˜์—ฌ ๋ฐ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, Signature๋Š” ์„œ๋ฒ„ ์ธก์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๋น„๋ฐ€ํ‚ค๊ฐ€ ์œ ์ถœ๋˜์ง€ ์•Š๋Š” ์ด์ƒ ๋ณตํ˜ธํ™”ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Signature๋Š” ํ† ํฐ์˜ ์œ„๋ณ€์กฐ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

โ—๏ธJWT ์žฅ์ 

  • ์ธ์ฆ ์ •๋ณด์— ๋Œ€ํ•œ ๋ณ„๋„์˜ ์ €์žฅ์†Œ๊ฐ€ ํ•„์š” ์—†์Šต๋‹ˆ๋‹ค. (์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ํ† ํฐ์„ ๊ฒ€์ฆ๋งŒ ํ•˜๋ฉด ok)
์„œ๋ฒ„์—์„œ ๊ฐ€์žฅ ํ”ผํ•ด์•ผ ํ•  ๊ฒƒ์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์กฐํšŒ์ž…๋‹ˆ๋‹ค.
์„œ๋ฒ„ ์ž์ฒด๊ฐ€ ์ฃฝ๋Š” ๊ฒฝ์šฐ๋„ ์žˆ์ง€๋งŒ, ๋Œ€๋ถ€๋ถ„ DB๊ฐ€ ํ„ฐ์ ธ์„œ ์„œ๋ฒ„๋„ ๊ฐ™์ด ์ฃฝ๋Š” ๊ฒฝ์šฐ๊ฐ€ ํ—ˆ๋‹คํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
  • Header์™€ Payload๋ฅผ ๊ฐ€์ง€๊ณ  Signature๋ฅผ ์ƒ์„ฑํ•˜๋ฏ€๋กœ ๋ฐ์ดํ„ฐ ์œ„๋ณ€์กฐ๋ฅผ ๋ง‰์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • JWT๋Š” ํ† ํฐ์— ๋Œ€ํ•œ ๊ธฐ๋ณธ ์ •๋ณด์™€ ์ „๋‹ฌํ•  ์ •๋ณด ๋ฐ ํ† ํฐ์ด ๊ฒ€์ฆ๋˜์—ˆ์Œ์„ ์ฆ๋ช…ํ•˜๋Š” ์„œ๋ช… ๋“ฑ ํ•„์š”ํ•œ ๋ชจ๋“  ์ •๋ณด๋ฅผ ์ž์ฒด์ ์œผ๋กœ ์ง€๋‹ˆ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

โ—๏ธJWT ๋‹จ์ 

  • ํ† ํฐ ์ธ์ฆ ๋ฐฉ์‹์€ stateless ํŠน์ง•์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์—, ํ† ํฐ์€ ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ๊ด€๋ฆฌํ•˜๊ณ  ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ํ† ํฐ ์ž์ฒด๋ฅผ ํƒˆ์ทจ๋‹นํ•˜๋ฉด ๋Œ€์ฒ˜ํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
    • JWT๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ , Access Token / Refresh Token์œผ๋กœ ์ด์ค‘์œผ๋กœ ๋‚˜๋ˆ„์–ด ์ธ์ฆ์„ ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๋Œ€์ฒ˜ํ•ฉ๋‹ˆ๋‹ค.
  • ํ† ํฐ ์ž์ฒด์— ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์–‘๋‚ ์˜ ๊ฒ€์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ค‘์š”ํ•œ ์ •๋ณด๋Š” ํŽ˜์ด๋กœ๋“œ์— ๋‹ด์ง€ ์•Š๋Š”๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

โ—๏ธJWT๋ฅผ ์ด์šฉํ•œ ์ธ์ฆ ๊ณผ์ •

JWT ์ธ์ฆ ๊ณผ์ •

1. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์„œ๋ฒ„์— ๋กœ๊ทธ์ธ ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

 

2. ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ID์™€ PW๋ฅผ ํ™•์ธ ํ›„, Header, Payload, Signature๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

+ Header, Payload, Signature๋ฅผ ํ•œ๋ฒˆ ๋” Base 64๋กœ ์ธ์ฝ”๋”ฉํ•˜์—ฌ Access Token๊ณผ Refresh Token๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฐœ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.

Access Token : ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๊ฐ–๊ณ ์žˆ๋Š” ์‹ค์ œ๋กœ ์œ ์ €์˜ ์ •๋ณด๊ฐ€ ๋‹ด๊ธด ํ† ํฐ์œผ๋กœ, ํด๋ผ์ด์–ธํŠธ์—์„œ ์š”์ฒญ์ด ์˜ค๋ฉด ์„œ๋ฒ„์—์„œ ํ•ด๋‹น ํ† ํฐ์— ์žˆ๋Š” ์ •๋ณด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ •๋ณด์— ๋งž๊ฒŒ ์‘๋‹ต์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

Refresh Token: ์ƒˆ๋กœ์šด Access Token์„ ์žฌ๋ฐœ๊ธ‰ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ํ† ํฐ. ํ•ด๋‹น ํ† ํฐ์€ ๋ณดํ†ต ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์œ ์ € ์ •๋ณด์™€ ๊ฐ™์ด ๊ธฐ๋ก๋ฉ๋‹ˆ๋‹ค.

 

3. ํด๋ผ์ด์–ธํŠธ๋Š” ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ JWT(Access Token, Refresh Token)์„ ๋กœ์ปฌ ์ €์žฅ์†Œ์— ์ €์žฅํ•˜๊ณ  ์ด ํ›„ ์š”์ฒญํ•  ๋•Œ ์•ก์„ธ์Šค ํ† ํฐ์„ ํ—ค๋”์— ๋„ฃ์–ด ์š”์ฒญ์„ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

 

4. ์„œ๋ฒ„์—์„œ๋Š” ์•ก์„ธ์Šค ํ† ํฐ์„ ๊ฒ€์ฆํ•˜๊ณ , ๋ฌธ์ œ๊ฐ€ ์—†์œผ๋ฉด ์‘๋‹ตํ•ด์ค๋‹ˆ๋‹ค.

 

5. ๋งŒ์•ฝ ํด๋ผ์ด์–ธํŠธ์˜ ์•ก์„ธ์Šค ํ† ํฐ์˜ ์œ ํšจ๊ธฐ๊ฐ„์ด ์ง€๋‚˜๋ฉด ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ์„ ์„œ๋ฒ„์— ์ „๋‹ฌํ•˜์—ฌ ์•ก์„ธ์Šค ํ† ํฐ์„ ์žฌ๋ฐœ๊ธ‰ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

 

6. ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ์„ ๊ฒ€์ฆํ•˜์—ฌ ์•ก์„ธ์Šค ํ† ํฐ์„ ์žฌ๋ฐœ๊ธ‰ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

 

๋กœ๊ทธ์•„์›ƒ์„ ํ•˜๋ฉด Access Token๊ณผ Refresh Token์„ ๋ชจ๋‘ ๋งŒ๋ฃŒ์‹œํ‚ต๋‹ˆ๋‹ค.

<์ฐธ๊ณ  ์ž๋ฃŒ>

 

 

JWT + Spring Security ๋ฅผ ์ด์šฉํ•œ ๋กœ๊ทธ์ธ ๊ณ ์ฐฐ - 1

https://spring.io/guides/topicals/spring-security-architecture Spring Security Architecture this topical is designed to be read and comprehended in under an hour, it provides broad coverage of a topic that is possibly nuanced or requires deeper understandi

onejunu.tistory.com

 

 

๐ŸŒ JWT ํ† ํฐ ์ธ์ฆ ์ด๋ž€? (์ฟ ํ‚ค vs ์„ธ์…˜ vs ํ† ํฐ)

Cookie / Session / Token ์ธ์ฆ ๋ฐฉ์‹ ์ข…๋ฅ˜ ๋ณดํ†ต ์„œ๋ฒ„๊ฐ€ ํด๋ผ์ด์–ธํŠธ ์ธ์ฆ์„ ํ™•์ธํ•˜๋Š” ๋ฐฉ์‹์€ ๋Œ€ํ‘œ์ ์œผ๋กœ ์ฟ ํ‚ค, ์„ธ์…˜, ํ† ํฐ 3๊ฐ€์ง€ ๋ฐฉ์‹์ด ์žˆ๋‹ค. JWT๋ฅผ ๋ฐฐ์šฐ๊ธฐ ์•ž์„œ ์šฐ์„  ์ฟ ํ‚ค์™€ ์„ธ์…˜์˜ ํ†ต์‹  ๋ฐฉ์‹์„ ๋ณต์Šตํ•ด

inpa.tistory.com

 

 

๐ŸŒ Access Token & Refresh Token ์›๋ฆฌ

Access Token & Refresh Token ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ๊ธฐ๋ณธ JWT ๋ฐฉ์‹์˜ ์ธ์ฆ(๋ณด์•ˆ) ๊ฐ•ํ™” ๋ฐฉ์‹์ธ Access Token & Refresh Token ์ธ์ฆ ๋ฐฉ์‹์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ๋‹ค. ๋จผ์ € JWT(Json Web Token) ์— ๋Œ€ํ•ด ์ž˜ ๋ชจ๋ฅด๋Š” ๋…์ž๋“ค์€ ๋‹ค์Œ ํฌ์Šค

inpa.tistory.com

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ