๐Ÿฅž BE
home

JWT

Date
2024/09/26
Category
Web
Tag
Spring Security
Detail

JWT

JWT(Json Web Token)๋Š” Json ํฌ๋งท์„ ์ด์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž์— ๋Œ€ํ•œ ์†์„ฑ์„ ์ €์žฅํ•˜๋Š” Claim ๊ธฐ๋ฐ˜์˜ Web Token์ด๋‹ค.
JWT๋Š”ย Header,ย Payload,ย Signature์˜ 3 ๋ถ€๋ถ„์œผ๋กœ ์ด๋ฃจ์–ด์ง€๋ฉฐ, Json ํ˜•ํƒœ์ธย ๊ฐ ๋ถ€๋ถ„์€ Base64๋กœ ์ธ์ฝ”๋”ฉย ๋˜์–ด ํ‘œํ˜„๋œ๋‹ค.
๋˜ํ•œย ๊ฐ๊ฐ์˜ ๋ถ€๋ถ„์„ ์ด์–ด ์ฃผ๊ธฐ ์œ„ํ•ด . ๊ตฌ๋ถ„์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌ๋ถ„ํ•œ๋‹ค.
์ถ”๊ฐ€๋กœ Base64๋Š” ์•”ํ˜ธํ™”๋œ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๊ณ , ๊ฐ™์€ ๋ฌธ์ž์—ด์— ๋Œ€ํ•ด ํ•ญ์ƒ ๊ฐ™์€ ์ธ์ฝ”๋”ฉ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

1. Header

ํ† ํฐ์˜ ํ—ค๋”๋Š”ย typ๊ณผย algย ๋‘ ๊ฐ€์ง€ ์ •๋ณด๋กœ ๊ตฌ์„ฑ๋œ๋‹ค. alg๋Š” ํ—ค๋”(Header)๋ฅผ ์•”ํ˜ธํ™” ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ณ , Signature๋ฅผ ํ•ด์‹ฑํ•˜๊ธฐ ์œ„ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ง€์ •ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. kid : ์„œ๋ช… ์‹œ ์‚ฌ์šฉํ•˜๋Š” ํ‚ค(Public/Private Key)๋ฅผ ์‹๋ณ„ํ•˜๋Š” ๊ฐ’
typ : ํ† ํฐ์˜ ํƒ€์ž…์„ ์ง€์ •.
alg : ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฐฉ์‹์„ ์ง€์ •ํ•˜๋ฉฐ, ์„œ๋ช…(Signature) ๋ฐ ํ† ํฐ ๊ฒ€์ฆ์— ์‚ฌ์šฉ ex) HS256(SHA256) ๋˜๋Š” RSA

2. Payload

ํ† ํฐ์˜ ํŽ˜์ด๋กœ๋“œ์—๋Š”ย ํ† ํฐ์—์„œ ์‚ฌ์šฉํ•  ์ •๋ณด์˜ ์กฐ๊ฐ๋“ค์ธ ํด๋ ˆ์ž„(Claim)์ด ๋‹ด๊ฒจ ์žˆ๋‹ค.
ํด๋ ˆ์ž„์€ ์ด 3๊ฐ€์ง€๋กœ ๋‚˜๋ˆ„์–ด์ง€๋ฉฐ,ย Json(Key/Value)ย ํ˜•ํƒœ๋กœย ๋‹ค์ˆ˜์˜ ์ •๋ณด๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
2.1. ๋“ฑ๋ก๋œ ํด๋ ˆ์ž„(Registered Claim)
2.2. ๊ณต๊ฐœ ํด๋ ˆ์ž„(Public Claim)
2.3. ๋น„๊ณต๊ฐœ ํด๋ ˆ์ž„(Private Claim)

JWT์—์„œ ์ •๋ณด ์ถ”์ถœํ•˜๊ธฐ!

Backend์—์„œ ์ง€์ •ํ•œ access_token๋ฅผ ๋ฐ›์€ ํ›„ name์ธ John Doe๋ฅผ ์ถ”์ถœ ํ•˜๋ ค๋ฉด ์•„๋ž˜์˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด jwt_decode๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ณด๋ฅผ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค!
โ†’ ํ•˜์ง€๋งŒ ์•„๋ž˜์˜ ๋‹จ์ ์„ ํ†ตํ•ด ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Œ!
const { access_token: accessToken } = res.data; const payload = jwt_decode(accessToken); const { name } = payload
Plain Text
๋ณต์‚ฌ
์—ฌ๊ธฐ์„œ jwt_decode๋Š” Base64Url๋กœ ์ธ์ฝ”๋”ฉ๋œ JWT ํ† ํฐ์„ ๋””์ฝ”๋”ฉํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ•˜๋Š” ๋ธŒ๋ผ์šฐ์ € ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค.

3. Signature

์„œ๋ช…(Signature)์€ ํ† ํฐ์„ ์ธ์ฝ”๋”ฉํ•˜๊ฑฐ๋‚˜ ์œ ํšจ์„ฑ ๊ฒ€์ฆ์„ ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ณ ์œ ํ•œ ์•”ํ˜ธํ™” ์ฝ”๋“œ์ด๋‹ค.
์„œ๋ช…(Signature)์€ ์œ„์—์„œ ๋งŒ๋“  ํ—ค๋”(Header)์™€ ํŽ˜์ด๋กœ๋“œ(Payload)์˜ ๊ฐ’์„ ๊ฐ๊ฐ BASE64๋กœ ์ธ์ฝ”๋”ฉํ•˜๊ณ , ์ธ์ฝ”๋”ฉํ•œ ๊ฐ’์„ ๋น„๋ฐ€ ํ‚ค๋ฅผ ์ด์šฉํ•ด ํ—ค๋”(Header)์—์„œ ์ •์˜ํ•œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ•ด์‹ฑ์„ ํ•˜๊ณ , ์ด ๊ฐ’์„ ๋‹ค์‹œ BASE64๋กœ ์ธ์ฝ”๋”ฉํ•˜์—ฌ ์ƒ์„ฑํ•œ๋‹ค.
์ƒ์„ฑ๋œ ํ† ํฐ์€ HTTP ํ†ต์‹ ์„ ํ•  ๋•Œ Authorization์ด๋ผ๋Š” key์˜ value๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์ผ๋ฐ˜์ ์œผ๋กœ value์—๋Š” Bearer์ด ์•ž์— ๋ถ™์—ฌ์ง„๋‹ค.
{ "Authorization": "Bearer {์ƒ์„ฑ๋œ ํ† ํฐ ๊ฐ’}", }
Plain Text
๋ณต์‚ฌ

JWT ๊ณ ๋ ค์‚ฌํ•ญ

โ€ข
Self-contained : ํ† ํฐ ์ž์ฒด์— ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์–‘๋‚ ์˜ ๊ฒ€์ด ๋  ์ˆ˜ ์žˆ๋‹ค.
โ€ข
ํ† ํฐ ๊ธธ์ด : ํ† ํฐ์˜ ํŽ˜์ด๋กœ๋“œ(Payload)์— 3์ข…๋ฅ˜์˜ ํด๋ ˆ์ž„์„ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ •๋ณด๊ฐ€ ๋งŽ์•„์งˆ์ˆ˜๋ก ํ† ํฐ์˜ ๊ธธ์ด๊ฐ€ ๋Š˜์–ด๋‚˜ ๋„คํŠธ์›Œํฌ์— ๋ถ€ํ•˜๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋‹ค.
โ€ข
Payload ์ธ์ฝ”๋”ฉ : ํŽ˜์ด๋กœ๋“œ(Payload) ์ž์ฒด๋Š” ์•”ํ˜ธํ™” ๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, BASE64๋กœ ์ธ์ฝ”๋”ฉ ๋œ ๊ฒƒ์ด๋‹ค. ์ค‘๊ฐ„์— Payload๋ฅผ ํƒˆ์ทจํ•˜์—ฌ ๋””์ฝ”๋”ฉํ•˜๋ฉด ๋ฐ์ดํ„ฐ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, JWE๋กœ ์•”ํ˜ธํ™”ํ•˜๊ฑฐ๋‚˜ Payload์— ์ค‘์š” ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์ง€ ์•Š์•„์•ผ ํ•œ๋‹ค.
โ†’ ์ด ๋‹จ์ ์„ ํ†ตํ•ด payload์— ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ  ์ถ”์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ข‹์€ ๋ฐฉ๋ฒ•์ด ์•„๋‹ˆ๋ผ๋Š” ๊ฒƒ์„ ๊นจ๋‹ฌ์•˜๋‹ค...
โ€ข
Stateless : JWT๋Š” ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๋ฒˆ ๋งŒ๋“ค์–ด์ง€๋ฉด ์ œ์–ด๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค. ์ฆ‰, ํ† ํฐ์„ ์ž„์˜๋กœ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฏ€๋กœ ํ† ํฐ ๋งŒ๋ฃŒ ์‹œ๊ฐ„์„ ๊ผญ ๋„ฃ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
โ€ข
Tore Token : ํ† ํฐ์€ ํด๋ผ์ด์–ธํŠธ ์ธก์—์„œ ๊ด€๋ฆฌํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ํ† ํฐ์„ ์ €์žฅํ•ด์•ผ ํ•œ๋‹ค.

Reference

[Spring Boot] Spring Security : JWT Auth (SpringBoot 3 ๋ฒ„์ „)
์ž‘๋…„ ๋ง, Spring 2์ ๋Œ€ ๋ฒ„์ „์˜ ์ง€์›์ด ๊ณต์‹ ์ค‘๋‹จ๋˜๋ฉด์„œ, ์ด์ œ ์›ฌ๋งŒํ•˜๋ฉด Spring 3 ๋ฒ„์ „๋Œ€๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์„ Spring ์ง„์˜์—์„œ ๊ถŒ์žฅํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ ์ค‘ Spring Security์˜ ๊ฒฝ์šฐ ๋ณ€ํ™”ํ•œ ๋‚ด์šฉ์ด ์กฐ๊ธˆ ์žˆ๋Š” ํŽธ์ด๋ผ ์ด ์ฐธ์— Spring Security๋ฅผ ์ด์šฉํ•œ JWT ์ธ์ฆ๊ณผ ์ธ๊ฐ€๋ฅผ ํ•ด๋‹น ๋ฒ„์ „๋Œ€์— ๋งž์ถ”๊ณ , ์ •๋ฆฌํ•˜์—ฌ ๋‹ค์‹œ ์ž‘์„ฑํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค. ์•„๋ž˜๋Š” Spring 2์ ๋Œ€ ๋ฒ„์ „์—์„œ์˜ JWT Security๋ฅผ ์„ค์ •ํ–ˆ๋˜ ํฌ์ŠคํŒ…๋“ค์ธ๋ฐ, 3์ ๋Œ€ ๋ฒ„์ „์—์„œ๋„ ๊ทผ๋ณธ์ ์ธ ๊ณผ์ •๋“ค์ด ๋ฐ”๋€ ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋งŒ์•ฝ 2์ ๋Œ€ ๋ฒ„์ „์—์„œ์˜ ์„ค์ •์ด ๊ถ๊ธˆํ•˜๋‹ค๋ฉด ์•„๋ž˜ ํฌ์ŠคํŒ…๋“ค์˜ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•˜์ž. ํšŒ์› ๊ฐ€์ž… : ํ•ด๋‹น ๋ถ€๋ถ„์€ Security์— ์˜์กดํ•˜์ง€ ์•Š์•„, ํšŒ์› ๊ฐ€์ž… ๋กœ์ง์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ์ฐธ๊ณ ํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค. https://sjh9708...