# [Security] OAuth2.0์„ ์ด์šฉํ•œ ์†Œ์…œ ๋กœ๊ทธ์ธ (๊ตฌ๊ธ€)
Study Repository

[Security] OAuth2.0์„ ์ด์šฉํ•œ ์†Œ์…œ ๋กœ๊ทธ์ธ (๊ตฌ๊ธ€)

by rlaehddnd0422

OAuth๋ž€?

OAuth๋Š” Open Authorization์˜ ์•ฝ์ž๋กœ, ์ธํ„ฐ๋„ท ์‚ฌ์šฉ์ž๋“ค์ด ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ œ๊ณตํ•˜์ง€ ์•Š๊ณ  ๋‹ค๋ฅธ ์›น์‚ฌ์ดํŠธ ์ƒ์˜ ์ž์‹ ๋“ค์˜ ์ •๋ณด์— ๋Œ€ํ•ด ์›น์‚ฌ์ดํŠธ๋‚˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ ‘๊ทผ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•  ์ˆ˜ ์žˆ๋Š” ๊ณตํ†ต์ ์ธ ์ˆ˜๋‹จ์œผ๋กœ์„œ ์‚ฌ์šฉ๋˜๋Š”, ์ ‘๊ทผ ์œ„์ž„์„ ์œ„ํ•œ ๊ฐœ๋ฐฉํ˜• ํ‘œ์ค€ ํ”„๋กœํ† ์ฝœ์ž…๋‹ˆ๋‹ค.

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

 

OAuth 2.0 ๊ตฌ์„ฑ์š”์†Œ

  1. Resource Owner : ์•ก์„ธ์Šคํ•  USER
  2. Authorization Server : USER์—๊ฒŒ Token์„ ๋ฐœ๊ธ‰ํ•ด์ฃผ๋Š” ๊ณต๊ธ‰์ž 
  3. Authorization Code : Access Token์„ ์–ป๊ธฐ ์œ„ํ•œ ์ž๊ฒฉ ์ฆ๋ช… 
  4. Access Token : ๋ณดํ˜ธ๋œ ์ž์›์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ ์ž๊ฒฉ ์ฆ๋ช…์„œ
  5. Scpoe : ํ† ํฐ์˜ ๊ถŒํ•œ์„ ์ง€์ •

 

OAuth ๋™์ž‘ ๊ณผ์ •

OAuth ์ธ์ฆ ๋ฐฉ์‹์€ ์ธ์ฆ์˜ ๊ณผ์ •์„ '๋‹ค๋ฅธ ์„œ๋น„์Šค์—๊ฒŒ ์œ„์ž„'ํ•˜๋Š” ์ธ์ฆ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

๊ตฌ๊ธ€์ด ํ•ด์ฃผ๋Š” ์ผ์€ ์›น ์‚ฌ์ดํŠธ ์‚ฌ์šฉ์ž๊ฐ€ ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์„ ํ†ตํ•ด ๊ตฌ๊ธ€์—๊ฒŒ ์ „์†กํ•œ ๊ตฌ๊ธ€ ๊ณ„์ • ์ •๋ณด๊ฐ€ ์œ ํšจํ•œ์ง€ ํ™•์ธํ•œ ํ›„,

์œ ํšจํ•˜๋‹ค๋ฉด ํ•ด๋‹นํ•˜๋Š” ๊ตฌ๊ธ€ ์œ ์ € ์ •๋ณด ์ค‘ ์ผ๋ถ€(์ด๋ฆ„, ์ฃผ์†Œ, ์•„์ด๋”” ๋“ฑ)๋ฅผ ๋‚ด ์›น์‚ฌ์ดํŠธ์— ์ œ๊ณตํ•ด์ฃผ๋Š” '์ธ์ฆ' ๊ณผ์ •๋งŒ์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

 

OAuth ๋™์ž‘ ๊ณผ์ •

์ฃผ์ฒด๋Š” ํฌ๊ฒŒ ์„ธ ๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

1. Resource Owner (USER) : ๋ฆฌ์†Œ์Šค ์†Œ์œ ์ž๋กœ, ์šฐ๋ฆฌ์˜ ์„œ๋น„์Šค๋ฅผ ์ด์šฉํ•˜๋ฉด์„œ, ๊ตฌ๊ธ€ ํŽ˜์ด์Šค ๋ถ ๋“ฑ์˜ ํ”Œ๋žซํผ์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ์†Œ์œ ํ•˜๊ณ  ์žˆ๋Š” ์‚ฌ์šฉ์ž.

2. Client : Resource Server์˜ ์ž์›์„ ์ด์šฉํ•˜๊ณ ์ž ํ•˜๋Š” ์„œ๋น„์Šค, ์ฆ‰ ์šฐ๋ฆฌ๊ฐ€ ๊ฐœ๋ฐœํ•œ ์„œ๋น„์Šค๋ผ๊ณ  ๋ณด๋ฉด ๋ฉ๋‹ˆ๋‹ค.

3. Authorization & Resource Server : Resource Owner(USER)๋ฅผ ์ธ์ฆํ•˜๊ณ , Client์—๊ฒŒ ์•ก์„ธ์Šค ํ† ํฐ์„ ๋ฐœ๊ธ‰ํ•ด์ฃผ๋Š” ์„œ๋ฒ„ ( ๊ตฌ๊ธ€, ๋„ค์ด๋ฒ„, ์นด์นด์˜ค ๋“ฑ )

 

Client๋Š” ์šฐ๋ฆฌ๊ฐ€ ๊ตฌํ˜„ํ•˜๋Š” ์„œ๋น„์Šค์ด๋ฏ€๋กœ Resource Owner์™€ ํ—ท๊ฐˆ๋ฆฌ์ง€ ์•Š๋„๋ก ํ•ฉ์‹œ๋‹ค.

 

๋™์ž‘ ๊ณผ์ •์€ ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

1. Resource Owner(USER) ๊ฐ€ '๊ตฌ๊ธ€๋กœ ๋กœ๊ทธ์ธ' ๋“ฑ ๋ฒ„ํŠผ์„ ํ†ตํ•ด ๋กœ๊ทธ์ธ์„ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

 

2. Client๋Š” OAuth ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ์ž์˜ ๋ธŒ๋ผ์šฐ์ €๋ฅผ Authorization Server๋กœ ๋ณด๋‚ด์•ผ ํ•˜๋Š”๋ฐ, ์ด ๋•Œ Authorization URL๋กœ Server๊ฐ€ ์ œ๊ณตํ•˜๋Š” client_ID, Redirect URI, Scope ๋“ฑ ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์œผ๋กœ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

 

3, 4. ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋นŒ๋“œํ•œ Authorization URL๋กœ ์ด๋™๋œ Resource Owner๋Š” ์ œ๊ณต๋œ ๋กœ๊ทธ์ธ ํŽ˜์ด์ง€์—์„œ ID ์™€ PW ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ์ธ์ฆํ•ฉ๋‹ˆ๋‹ค.

 

5, 6. ์ธ์ฆ(๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ)์— ์„ฑ๊ณตํ•˜๋ฉด , Authorization Server๋Š” ์ œ๊ณต๋œ Redirect URI๋กœ ์‚ฌ์šฉ์ž๋ฅผ ๋ฆฌ๋‹ค์ด๋ ‰์…˜ ์‹œํ‚ต๋‹ˆ๋‹ค. ( ์ด ๊ณผ์ •์—์„œ Authorization Code๋ฅผ ํฌํ•จํ•˜์—ฌ ๋ฆฌ๋‹ค์ด๋ ‰์…˜, ๊ตฌ๊ธ€์˜ ๊ฒฝ์šฐ ์ฝ”๋“œ๋ฅผ ์ฟผ๋ฆฌ์ŠคํŠธ๋ง์— ํฌํ•จํ•ฉ๋‹ˆ๋‹ค.)

 

7, 8. Client๋Š” Authorization Server์— Authorization Code, Client_ID, Client Secret์„ ์ „๋‹ฌํ•˜๊ณ , Access Token์„ ๋ฆฌํ„ด๋ฐ›์Šต๋‹ˆ๋‹ค.

 

9. ์œ„ ๊ณผ์ •์„ ์„ฑ๊ณต์ ์œผ๋กœ ๋งˆ์น˜๋ฉด ๋กœ๊ทธ์ธ ์„ฑ๊ณต

 

10. Resource Owner๊ฐ€ Access Token์„ ํ†ตํ•ด Client์—๊ฒŒ ์„œ๋น„์Šค๋ฅผ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.

 

11 ~ 13 . Client๋Š” ๋ฐœ๊ธ‰ํ•˜๊ณ  ์ €์žฅํ•ด๋‘” Access Token์„ ์‚ฌ์šฉํ•˜์—ฌ Resource Owner์—๊ฒŒ ์ œํ•œ๋œ ๋ฆฌ์†Œ์Šค์— ์ ‘๊ทผ์„ ํ•˜์šฉํ•˜๊ณ , ์„œ๋น„์Šค๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

 

OAuth์— ๊ด€ํ•ด ๋งŽ์€ ์ž๋ฃŒ๋“ค์ด ์žˆ์—ˆ์ง€๋งŒ, ๊ทธ ์ค‘ ๊ฐ€์žฅ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•œ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์˜€์Šต๋‹ˆ๋‹ค.
์ฐธ๊ณ ์ž๋ฃŒ : https://hudi.blog/oauth-2.0/

์ด์ œ ์œ„ ๋™์ž‘๊ณผ์ •์„ ํ† ๋Œ€๋กœ ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ์„ ๋„์ž…ํ•ด๋ด…์‹œ๋‹ค.

 

Resource Server API ๋ฐœ๊ธ‰

1. Google API ์ฝ˜์†” ์ ‘์†

https://console.cloud.google.com/apis/dashboard?hl=ko 

 

Google ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ

๋กœ๊ทธ์ธ Google ํด๋ผ์šฐ๋“œ ํ”Œ๋žซํผ์œผ๋กœ ์ด๋™

accounts.google.com

2. Google Cloud ๋กœ๊ณ  ์˜† ๋ฐ•์Šค ํด๋ฆญ -> ์ƒˆ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ2

3. ํ”„๋กœ์ ํŠธ ์ด๋ฆ„ ์„ค์ • ๋ฐ ๋งŒ๋“ค๊ธฐ

4. OAuth ๋™์˜ ํ™”๋ฉด์— ์‚ฌ์šฉ์ž ์œ ํ˜• - '์™ธ๋ถ€' ์„ค์ •

5. ์‚ฌ์šฉ์ž ์ธ์ฆ ์ •๋ณด์— ๋“ค์–ด๊ฐ€, + ์‚ฌ์šฉ์ž ์ธ์ฆ ์ •๋ณด ๋งŒ๋“ค๊ธฐ ํด๋ฆญ ๋ฐ OAuth ํด๋ผ์ด์–ธํŠธ ID ์ƒ์„ฑ, ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์œ ํ˜•์œผ๋กœ ์ƒ์„ฑ

 

 

6. ๋ฆฌ๋‹ค์ด๋ ‰์…˜ URI ๋“ฑ๋ก

  • ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ์„ฑ๊ณต ์‹œ ์—ฌ๊ธฐ์— ๋“ฑ๋กํ•œ ๋ฆฌ๋‹ค์ด๋ ‰์…˜ URI๋กœ Authorization Code๋ฅผ ๋ฐœ๊ธ‰ํ•ฉ๋‹ˆ๋‹ค.

7.  Client ID, Client Secret ๋ฐœ๊ธ‰ 

  • ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ์‹œ Authorization URL์— ํ•จ๊ป˜ ๋ณด๋‚ผ ์ •๋ณด๋“ค์„ ๋ฐœ๊ธ‰๋ฐ›์Šต๋‹ˆ๋‹ค.

 

๋กœ๊ทธ์ธ ํผ์— '๊ตฌ๊ธ€๋กœ ๋กœ๊ทธ์ธ' ์ƒ์„ฑ ๋ฐ ๋ฆฌ๋‹ค์ด๋ ‰์…˜ href ์ถ”๊ฐ€

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>๋กœ๊ทธ์ธ ํŽ˜์ด์ง€</title>
</head>
<body>
<h1>๋กœ๊ทธ์ธ ํŽ˜์ด์ง€</h1>
<hr/>
<!-- ์‹œํ๋ฆฌํ‹ฐ๋Š” x-www-form-url-encoded ํƒ€์ž…๋งŒ ์ธ์‹ -->
<form action="/login" method="post">
    <input type="text" name="username" placeholder="Username" />
    <input type="password" name="password" placeholder="Password"/>
    <button>๋กœ๊ทธ์ธ</button>
</form>
<a href="/oauth2/authorization/google">๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ</a>
<a href="/joinForm">ํšŒ์›๊ฐ€์ž…์„ ์•„์ง ํ•˜์ง€ ์•Š์œผ์…จ๋‚˜์š”?</a>
</body>
</html>
  • ๋กœ๊ทธ์ธ ํผ์—์„œ '๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ'์„ ํด๋ฆญํ•˜๋ฉด Resource Server(Google)์—์„œ ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ํผ(Authorization URL)๋กœ ์ด๋™ ํ›„ ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ์— ์„ฑ๊ณตํ•˜๋ฉด ๋ฆฌ๋‹ค์ด๋ ‰์…˜ URI๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค.
  • Authorization URL์„ ํ†ตํ•ด ๋กœ๊ทธ์ธ ํ•  ๋•Œ ํ•„์š”ํ•œ ๋ฐœ๊ธ‰๋ฐ›์€ ID์™€ Secret์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ •๋ณด๋“ค์€ yml์— ๋ณ„๋„๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 
  • ์ด ๋•Œ ๋ฆฌ๋‹ค์ด๋ ‰์…˜ URI๋กœ Authorization Code๋ฅผ ํ•จ๊ป˜ ๋ณด๋ƒ…๋‹ˆ๋‹ค.

 

application.yml์— ๋ฐœ๊ธ‰ ๋ฐ›์€ ์ •๋ณด ์ž…๋ ฅ

security:
  oauth2:
    client:
      registration:
        google:
          client-id: ~~
          client-secret: ~~
          scope:
            - email
            - profile
  • Authorizaiton URL(๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ ํผ)์— ํ•จ๊ป˜ ์ „์†กํ•  Resource Owner์˜ ์ •๋ณด๋ฅผ application.yml์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

Security Filter์— OAuth ์˜ต์…˜ ์ถ”๊ฐ€

.and()
.oauth2Login()
.loginPage("/loginForm")
  • OAuth๋ฅผ ์‚ฌ์šฉํ•œ ๋กœ๊ทธ์ธ ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ  OAuth ๋กœ๊ทธ์ธ ์˜ต์…˜์„ ์‚ฌ์šฉํ•˜๋Š” loginPage ๋˜ํ•œ ์„ค์ •ํ•ด ์ค์‹œ๋‹ค.

์ด๋ ‡๊ฒŒ ํ•ด์„œ ๋กœ๊ทธ์ธ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ ๋กœ๊ทธ์ธ(์ธ์ฆ) ์ ˆ์ฐจ๋งŒ ์™„๋ฃŒ ํ–ˆ์„ ๋ฟ, ์•„์ง OAuth Owner ์‚ฌ์šฉ์ž์— ๋Œ€ํ•œ ์ธ๊ฐ€ ์ฒ˜๋ฆฌ๋Š” ํ•˜์ง€ ๋ชปํ•œ ์ƒํ™ฉ์ž…๋‹ˆ๋‹ค.

( ์•„์ง ์ ‘๊ทผ ์ œํ•œ์ด ๊ฑธ๋ฆฐ ๋ฆฌ์†Œ์Šค์— ๋Œ€ํ•œ ์ ‘๊ทผ์€ ๋ถˆ๊ฐ€๋Šฅํ•œ ์ƒํ™ฉ )

๋˜ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๊ตฌ๊ธ€๋กœ ๋กœ๊ทธ์ธํ•œ ํšŒ์›์˜ ์ •๋ณด ์ €์žฅ ๋˜ํ•œ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. 

 

๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ OAuth ๋กœ๊ทธ์ธ ํ›„์ฒ˜๋ฆฌ(OAuth Resource Owner์— ๋Œ€ํ•œ ์ ‘๊ทผ ๊ถŒํ•œ ์ฒ˜๋ฆฌ ๋ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ํšŒ์› ์ €์žฅ)์„ ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

<์ •๋ฆฌ>

  • OAuth2.0์€ Resource Owner, Client, Resource Server ์„ธ ๊ฐ€์ง€ ์ฃผ์ฒด์˜ ์ƒํ˜ธ์ž‘์šฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.
  • OAuth2.0์„ ํ†ตํ•ด ๋กœ๊ทธ์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Resource Server๋กœ๋ถ€ํ„ฐ Client_ID, Client_Secret์„ ๋ฐœ๊ธ‰๋ฐ›๊ณ , Ower๊ฐ€ OAuth ๋กœ๊ทธ์ธ ์„ฑ๊ณต ์‹œ(OAuth ID, PW, Client_ID, Client_Secret์„ ํ†ตํ•œ) Resource Server์—์„œ Resource Owner์—๊ฒŒ ์ „๋‹ฌํ•ด์ค„ code๋ฅผ ์ „๋‹ฌํ•  Rediret_URI๋ฅผ ์„ค์ •ํ•ด ์ฃผ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • OAuth2.0์˜ ๋™์ž‘๊ณผ์ •์„ ์ž˜ ํŒŒ์•…ํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค.

 

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

 

OAuth 2.0 ๊ฐœ๋…๊ณผ ๋™์ž‘์›๋ฆฌ

2022๋…„ 07์›” 13์ผ์— ์ž‘์„ฑํ•œ ๊ธ€์„ ๋ณด์ถฉํ•˜์—ฌ ์ƒˆ๋กœ ํฌ์ŠคํŒ…ํ•œ ๊ธ€์ด๋‹ค. OAuth ๋“ฑ์žฅ ๋ฐฐ๊ฒฝ ์šฐ๋ฆฌ์˜ ์„œ๋น„์Šค๊ฐ€ ์‚ฌ์šฉ์ž๋ฅผ ๋Œ€์‹ ํ•˜์—ฌ ๊ตฌ๊ธ€์˜ ์บ˜๋ฆฐ๋”์— ์ผ์ •์„ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜, ํŽ˜์ด์Šค๋ถ, ํŠธ์œ„ํ„ฐ์— ๊ธ€์„ ๋‚จ๊ธฐ๋Š” ๊ธฐ๋Šฅ์„

hudi.blog

 

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

Study Repository

rlaehddnd0422

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