๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Spring๐Ÿƒ

OAuth2

728x90

์ถœ์ฒ˜ : ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„

 

Open Standard for Authorization

์ฆ‰, ๊ฐœ๋ฐฉํ˜• Authorization ์˜ ํ‘œ์ค€์ด๋ฉฐ API ํ—ˆ๊ฐ€(Authorize)๋ฅผ ๋ชฉ์ ์œผ๋กœ

JSON ํ˜•์‹์œผ๋กœ ๊ฐœ๋ฐœ๋œ HTTP ๊ธฐ๋ฐ˜์˜ ๋ณด์•ˆ ํ”„๋กœํ† ์ฝœ.

์‚ฌ์šฉ์ž๋“ค์ด ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์›น์‚ฌ์ดํŠธ ๋ฐ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๊ณ 

์ ‘๊ทผ ๊ถŒํ•œ์„ ๋ถ€์—ฌ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ๊ณตํ†ต์  ์ˆ˜๋‹จ์œผ๋กœ์„œ ์‚ฌ์šฉ ๋˜์–ด์ง€๋Š” ๊ธฐ์ˆ 

๋‹ค์–‘ํ•œ ํด๋ผ์ด์–ธํŠธ ํ™˜๊ฒฝ์— ์ ํ•ฉํ•œ ์ธ์ฆ(Authentication) ๋ฐ ์ธ๊ฐ€(Authorization) ์˜ ์œ„์ž„ ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•˜๊ณ 

๊ทธ ๊ฒฐ๊ณผ๋กœ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ ‘๊ทผ ํ† ํฐ (Access Token) ์„ ๋ฐœ๊ธ‰ํ•˜๋Š” ๊ฒƒ์— ๋Œ€ํ•œ ๊ตฌ์กฐ

 

๊ฐ•์˜ : ์นด์นด์˜ค๋ฅผ ์ด์šฉํ•œ OAuth2 ๋กœ๊ทธ์ธ ์„œ๋น„์Šค ๊ตฌํ˜„

1. ํšŒ์›๊ฐ€์ž… ์„ค๊ณ„

1) User ํ…Œ์ด๋ธ”

์ปฌ๋Ÿผ ์ด๋ฆ„ ์ปฌ๋Ÿผ ํƒ€์ž… ์ค‘๋ณต ํ—ˆ์šฉ ์„ค๋ช…
id Long X ํ…Œ์ด๋ธ” ID(PK)
username String X ํšŒ์› ID
password String O ๋น„๋ฐ€๋ฒˆํ˜ธ
email String X ์ด๋ฉ”์ผ ์ฃผ์†Œ
role String O ์—ญํ•  :
์‚ฌ์šฉ์ž - User
๊ด€๋ฆฌ์ž - Admin

์นด์นด์˜ค ์‚ฌ์šฉ์ž ์ •๋ณด Json : 1. kakoId, 2. nickname, 3.email

{
  "id": 1632335751,
  "properties": {
    "nickname": "๋ฅดํƒ„์ด",
    "profile_image": "http://k.kakaocdn.net/...jpg",
    "thumbnail_image": "http://k.kakaocdn.net/...jpg"
  },
  "kakao_account": {
    "profile_needs_agreement": false,
    "profile": {
      "nickname": "๋ฅดํƒ„์ด",
      "thumbnail_image_url": "http://k.kakaocdn.net/...jpg",
      "profile_image_url": "http://k.kakaocdn.net/...jpg"
    },
    "has_email": true,
    "email_needs_agreement": false,
    "is_email_valid": true,
    "is_email_verified": true,
    "email": "letan@sparta.com"
  }
}

2) ํ…Œ์ด๋ธ” ์„ค๊ณ„ ์˜ต์…˜

(1) ์นด์นด์˜ค User(KakaoUser) ํ…Œ์ด๋ธ”์„ ํ•˜๋‚˜ ๋” ๋งŒ๋“ ๋‹ค

์žฅ์  : ๊ฒฐํ•ฉ๋„๊ฐ€ ๋‚ฎ์•„์ง„๋‹ค ~ ๊ฐ ํ…Œ์ด๋ธ”์˜ ๋ณ€ํ™”์— ์„œ๋กœ ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค

๋‹จ์  : ๊ตฌํ˜„ ๋‚œ์ด๋„ ์ƒ์Šน

(2) ๊ธฐ์กด User ํ…Œ์ด๋ธ”์— ์นด์นด์˜ค User ์ปฌ๋Ÿผ ์ถ”๊ฐ€

์žฅ์  : ๊ตฌํ˜„ ๋‹จ์ˆœ

๋‹จ์  : ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์•„์ง„๋‹ค ~

ํผ ๋กœ๊ทธ์ธ ๋ฐฉ์‹์„ ์‚ฌ์šฉ, ์นด์นด์˜ค ์‚ฌ์šฉ์ž์˜ username, password ์ •๋ณด๋ฅผ ๋ฐ›์•„ ๋กœ๊ทธ์ธํ•˜๋Š” ๊ฒฝ์šฐ ๋ฌธ์ œ ๋ฐœ์ƒ

-> 2) ๋ฐฉ์‹ ์ฑ„ํƒ

์ปฌ๋Ÿผ ์ด๋ฆ„ ์ปฌ๋Ÿผ ํƒ€์ž… ์ค‘๋ณต ํ—ˆ์šฉ ์„ค๋ช… ์นด์นด์˜ค ์‚ฌ์šฉ์ž
id Long X ํ…Œ์ด๋ธ” ID(PK) ํ…Œ์ด๋ธ” ID
username String X ํšŒ์› ID nickname
password String O ๋น„๋ฐ€๋ฒˆํ˜ธ UUID(๋žœ๋ค ๋ฌธ์ž์—ด)
email String X ์ด๋ฉ”์ผ ์ฃผ์†Œ email
role String O ์—ญํ•  :
์‚ฌ์šฉ์ž - User
๊ด€๋ฆฌ์ž - Admin
User
kakaoId String(Nullable) X ์นด์นด์˜ค ๋กœ๊ทธ์ธ ID kakaoId

UUID : ํผ ๋กœ๊ทธ์ธ ๋ฐฉ์‹์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด์„œ

2. ์นด์นด์˜ค ๋กœ๊ทธ์ธ ์‚ฌ์ „์ž‘์—…

1) User ์—”ํ‹ฐํ‹ฐ

- kakaoId ์ปฌ๋Ÿผ ์ถ”๊ฐ€

- kakao ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋ฐ›๋Š” ์ƒ์„ฑ์ž ์ถ”๊ฐ€

- ์นด์นด์˜ค ์•„์ด๋”” ์—…๋ฐ์ดํŠธ ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€

2) UserRepository

- ์ถ”์ƒ๋ฉ”์„œ๋“œ findByKakaoId(Long id), findByEmail(String email) ์ถ”๊ฐ€

3) KakaoUserInfoDto : id(kakaoId), email, nickname์„ ๋ฐ›๋Š” Dto ์ƒ์„ฑ

4) KakaoService ์ƒ์„ฑ : ์—‘์„ธ์Šค ํ† ํฐ ์š”์ฒญ, ํ† ํฐ์œผ๋กœ ์นด์นด์˜ค api ํ˜ธ์ถœ, kakaoId๋กœ ํšŒ์›๊ฐ€์ž…, JWT ํ† ํฐ ๋ฐ˜ํ™˜์„ ๊ฐ ๋ฉ”์„œ๋“œ์™€ ๋กœ๊ทธ์ธ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” kakaoLogin ๋ฉ”์„œ๋“œ ์ž‘์„ฑ

3. ์นด์นด์˜ค ์ธ๊ฐ€์ฝ”๋“œ ๋ฐ›๊ธฐ

์ธ๊ฐ€์ฝ”๋“œ ์š”์ฒญ๋ฐฉ๋ฒ• : 

https://kauth.kakao.com/oauth/authorize?client_id=505ea73b6160cd5b3cb332757130449c&redirect_uri=http://localhost:8080/api/user/kakao/callback&response_type=code

-> URL์— REST API ํ‚ค ์ž…๋ ฅ

ํ๋ฆ„ : ์‚ฌ์šฉ์ž๊ฐ€ ์นด์นด์˜ค ์•„์ด๋””๋ฅผ ํ†ตํ•œ ์„œ๋น„์Šค ๊ฐ€์ž…์— ๋™์˜ํ•˜๊ณ  ๊ฐ€์ž…ํ•˜๋ฉด Redirect URI(callback)๋กœ ์ธ๊ฐ€ ์ฝ”๋“œ ์ „๋‹ฌ ํ›„ UserController์—์„œ ์ฒ˜๋ฆฌ

UserController

- ์ธ๊ฐ€์ฝ”๋“œ์™€ HttpServletResponse ํƒ€์ž…์˜ request๋ฅผ ์„œ๋น„์Šค์˜ kakaoLogin๋กœ ์ „๋‹ฌ

- ์ฟ ํ‚ค ์ƒ์„ฑ ํ›„ ์ง์ ‘ ๋ธŒ๋ผ์šฐ์ €์— Set (JWT ํ† ํฐ์œผ๋กœ ๋Œ€์ฒด ๊ฐ€๋Šฅํ•œ ๋ถ€๋ถ„์ธ๋“ฏ)

KakaoService

- ์ธ๊ฐ€์ฝ”๋“œ๋กœ ์•ก์„ธ์Šคํ† ํฐ ์š”์ฒญ ๋ฉ”์„œ๋“œ ์ž‘์„ฑ

Http Header, Body ์ƒ์„ฑ ํ›„ Http ์š”์ฒญ ๋ณด๋‚ด๊ธฐ -> Http ์‘๋‹ต์„ Json ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ ํ›„ ์•ก์„ธ์Šคํ† ํฐ ํŒŒ์‹ฑ

- ์•ก์„ธ์Šค ํ† ํฐ์œผ๋กœ ์นด์นด์˜ค ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ€์ ธ์˜ค๋Š” ๋ฉ”์„œ๋“œ ์ž‘์„ฑ

Http Header ์ƒ์„ฑ ํ›„ Http ์š”์ฒญ ๋ณด๋‚ด๊ธฐ

- ์นด์นด์˜ค ์‚ฌ์šฉ์ž ์ •๋ณด๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ํ•œ ํšŒ์›๊ฐ€์ž… ๋ฉ”์„œ๋“œ ์ž‘์„ฑ

์ค‘๋ณต ์•„์ด๋”” ๋ฐ ์ด๋ฉ”์ผ ํ™•์ธ ํ›„ ๊ธฐ์กด ๊ฐ€์ž…์ž : ๊ธฐ์กด ์ •๋ณด์— kakaoId๋ž€์— ์ •๋ณด ์ถ”๊ฐ€/ ์‹ ๊ทœ ๊ฐ€์ž…์ž : ์‹ ๊ทœ๊ฐ€์ž…

~ User ํƒ€์ž… ๊ฐ์ฒด ๋ฐ˜ํ™˜

728x90