๐Ÿ“•CS

JWT ํ† ํฐ ๊ตฌ์กฐ์™€ ํ™œ์šฉ ๋ฐฉ๋ฒ•

stonesy 2024. 12. 6. 15:43
728x90

๐Ÿ“ŒJWT ํ† ํฐ

JWT(JSON Web Token)์ด๋ž€ ์›น ํ‘œ์ค€์œผ๋กœ, ์ฃผ๋กœ ์‚ฌ์šฉ์ž ์ธ์ฆ ๋ฐ ์ •๋ณด ๊ตํ™˜์— ์‚ฌ์šฉ๋œ๋‹ค. JWT๋Š” ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๊ตฌ์„ฑ๋˜๋ฉฐ, ๊ฐ๊ฐ ํ—ค๋”, ํŽ˜์ด๋กœ๋“œ, ์‹œ๊ทธ๋‹ˆ์ฒ˜๋กœ ๋‚˜๋‰œ๋‹ค. ํ—ค๋”๋Š” ํ† ํฐ์˜ ์œ ํ˜•๊ณผ ์„œ๋ช… ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํฌํ•จํ•˜๋ฉฐ, ํŽ˜์ด๋กœ๋“œ๋Š” ์‚ฌ์šฉ์ž ์ •๋ณด์™€ ๊ฐ™์€ ํด๋ ˆ์ž„์„ ํฌํ•จํ•˜๋ฉฐ, ์‹œ๊ทธ๋‹ˆ์ฒ˜๋Š” ํ† ํฐ์˜ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณด์žฅํ•œ๋‹ค.

JWT๋Š” ์ฃผ๋กœ OAuth์™€ ๊ฐ™์€ ์ธ์ฆ ์‹œ์Šคํ…œ์—์„œ ์‚ฌ์šฉ๋˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ์™€ ์„œ๋ฒ„ ๊ฐ„์˜ ์•ˆ์ „ํ•œ ์ •๋ณด ๊ตํ™˜์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด JWT๋Š” ์ž์ฒด์ ์œผ๋กœ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์–ด ์„œ๋ฒ„์˜ ์ƒํƒœ๋ฅผ ์œ ์ง€ํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

ํ—ค๋” ํ† ํฐ์˜ ์œ ํ˜•๊ณผ ์„œ๋ช… ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํฌํ•จํ•œ๋‹ค. {"alg": "HS256", "typ": "JWT"}
ํŽ˜์ด๋กœ๋“œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ํฌํ•จํ•œ๋‹ค.  {"sub": "1234567890", "name": "John Doe", "iat": 1516239022}
์‹œ๊ทธ๋‹ˆ์ฒ˜ ํ† ํฐ์˜ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณด์žฅํ•œ๋‹ค. ํ—ค๋”์™€ ํŽ˜์ด๋กœ๋“œ๋ฅผ ์ธ์ฝ”๋”ฉํ•œ ํ›„, ์‹œํฌ๋ฆฟ ํ‚ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑํ•œ๋‹ค. ์ด๋Š” ํ† ํฐ์˜ ๋ฌด๊ฒฐ์„ฑ์„ ๋ณด์žฅํ•œ๋‹ค.

 

๐Ÿ“ŒJWT ํ† ํฐ ๋ฐœ๊ธ‰๊ณผ ๊ฒ€์ฆ

JWT๋Š” ์ฃผ๋กœ ์„œ๋ฒ„์—์„œ ๋ฐœ๊ธ‰๋˜๋ฉฐ, ํด๋ผ์ด์–ธํŠธ๋Š” ์ด๋ฅผ ์ €์žฅํ•˜๊ณ  ์š”์ฒญ ์‹œ๋งˆ๋‹ค ์„œ๋ฒ„์— ์ „๋‹ฌํ•œ๋‹ค.

๋ฐœ๊ธ‰ ๊ณผ์ •์€: ํ—ค๋”์™€ ํŽ˜์ด๋กœ๋“œ๋ฅผ JSON์œผ๋กœ ๋งŒ๋“ค๊ณ , ์ด๋ฅผ Base64URL๋กœ ์ธ์ฝ”๋”ฉํ•œ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ์‹œํฌ๋ฆฟ ํ‚ค์™€ ๊ฒฐํ•ฉํ•˜์—ฌ ์‹œ๊ทธ๋‹ˆ์ฒ˜๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

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

 

* Spring์—์„œ๋Š” ๋ณดํ†ต jjwt (Java JWT) ๋˜๋Š” Spring Security์™€ ํ•จ๊ป˜ JWT๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ฆ ๋ฐ ๊ถŒํ•œ ๊ด€๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

1. JWT ๋ฐœ๊ธ‰
์‚ฌ์šฉ์ž๊ฐ€ ๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•˜๋ฉด ์„œ๋ฒ„(SPRING)๊ฐ€ JWT๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.๋ฐœ๊ธ‰ ์‹œ userId, roles ๋“ฑ์˜ ์ •๋ณด๋ฅผ ํฌํ•จํ•˜๋ฉฐ, ๋น„๋ฐ€ ํ‚ค(secretKey)๋กœ ์„œ๋ช…ํ•ฉ๋‹ˆ๋‹ค.
2. JWT ์ €์žฅ
ํด๋ผ์ด์–ธํŠธ๋Š” ๋ฐœ๊ธ‰๋ฐ›์€ JWT๋ฅผ HTTP ํ—ค๋”(์˜ˆ: Authorization: Bearer <JWT>) ๋˜๋Š” ์ฟ ํ‚ค์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.์‚ฌ์šฉ์ž ์š”์ฒญ๋งˆ๋‹ค ํ•ด๋‹น ํ† ํฐ์„ ์„œ๋ฒ„๋กœ ์ „๋‹ฌํ•˜์—ฌ ์ธ์ฆ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
3. JWT ๊ฒ€์ฆ
ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ณด๋‚ธ JWT๋ฅผ ์„œ๋ฒ„์—์„œ ํ™•์ธํ•˜๊ณ , ์„œ๋ช…๊ณผ ๋งŒ๋ฃŒ ์‹œ๊ฐ„(expiresIn)์„ ๊ฒ€์ฆํ•ฉ๋‹ˆ๋‹ค.์œ ํšจํ•˜๋‹ค๋ฉด ํ† ํฐ์˜ payload์—์„œ ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ์ถ”์ถœํ•˜๊ณ  ์ธ์ฆ ์ƒํƒœ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

 

*Base64URL๋กœ ์ธ์ฝ”๋”ฉ ๋˜๋Š” ์ด์œ 

  • ๋ชฉ์ :
    ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ์ดํ„ฐ๋ฅผ ํ…์ŠคํŠธ ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ฑฐ๋‚˜, URL-safe(์•ˆ์ „ํ•œ URL) ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•จ.
  • ๋ฐฉ์‹:
    Base64 ์ธ์ฝ”๋”ฉ์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, URL์—์„œ ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š” +, /, =๋ฅผ ๊ฐ๊ฐ -, _, ์ œ๊ฑฐ๋กœ ๋Œ€์ฒดํ•ฉ๋‹ˆ๋‹ค.
    ์ด ๋ฐฉ์‹์€ Base64์˜ ๋ณ€ํ˜•์œผ๋กœ URL์—์„œ ์•ˆ์ „ํ•˜๊ฒŒ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

 

๐Ÿ“ŒJWT ํ† ํฐ ๋ฐœ๊ธ‰๊ณผ ๊ฒ€์ฆ

*JWT๋ฅผ URL์— ํฌํ•จํ•˜๋Š” ๊ฒƒ์ด ์ ํ•ฉํ•œ ๊ฒฝ์šฐ

1. OAuth 2.0 ๋ฆฌ๋‹ค์ด๋ ‰์…˜ ์‹œ๋‚˜๋ฆฌ์˜ค

OAuth 2.0์˜ Authorization Code Grant ๋ฐฉ์‹์—์„œ๋Š”, ์‚ฌ์šฉ์ž๊ฐ€ ์ธ์ฆ์„ ์™„๋ฃŒํ•œ ํ›„ ์ธ์ฆ ์„œ๋ฒ„๊ฐ€ ๋ฆฌ๋‹ค์ด๋ ‰์…˜ URL์„ ํ†ตํ•ด Authorization Code๋ฅผ ํด๋ผ์ด์–ธํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. (๋ฆฌ๋‹ค์ด๋ ‰์…˜ ์‘๋‹ต์„ ๋ฐ›์œผ๋ฏ€๋กœ URL์„ ํ†ตํ•ด์„œ ํ† ํฐ์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ์‹์ด๋‹ค.

 https://example.com/callback?code=abc123&state=xyz

 

2. ๋น„๋ฐ€๋ฒˆํ˜ธ ์žฌ์„ค์ • ๋˜๋Š” ์ด๋ฉ”์ผ ์ธ์ฆ ๋งํฌ ๋“ฑ

(์ถ”ํ›„ ์—…๋ฐ์ดํŠธ ์˜ˆ์ •)

 

๐Ÿ“Œ์ฐธ๊ณ 

https://f-lab.kr/insight/jwt-token-structure-and-usage-20240603

 

JWT ํ† ํฐ์˜ ๊ตฌ์กฐ์™€ ํ™œ์šฉ ๋ฐฉ๋ฒ•

์ด ๊ธ€์—์„œ๋Š” JWT์˜ ๊ตฌ์กฐ์™€ ๊ฐ ๋ถ€๋ถ„์˜ ์—ญํ• , ๊ทธ๋ฆฌ๊ณ  ์‹ค์ œ ์‚ฌ์šฉ ์˜ˆ์ œ๋ฅผ ํ†ตํ•ด JWT์˜ ํ™œ์šฉ ๋ฐฉ๋ฒ•์„ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. JWT๋Š” ์‚ฌ์šฉ์ž ์ธ์ฆ ๋ฐ ์ •๋ณด ๊ตํ™˜์— ๋งค์šฐ ์œ ์šฉํ•œ ๋„๊ตฌ๋กœ, ์ด๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์•ˆ์ „ํ•œ ์ธ์ฆ ์‹œ์Šค

f-lab.kr

 

728x90