Browse Source

๐ŸŒ Add Korean translation for `docs/ko/docs/tutorial/security/get-current-user.md` (#5737)

pull/11016/head
DoHyun Kim 1 year ago
committed by GitHub
parent
commit
0ec0df5090
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 151
      docs/ko/docs/tutorial/security/get-current-user.md

151
docs/ko/docs/tutorial/security/get-current-user.md

@ -0,0 +1,151 @@
# ํ˜„์žฌ ์‚ฌ์šฉ์ž ๊ฐ€์ ธ์˜ค๊ธฐ
์ด์ „ ์žฅ์—์„œ (์˜์กด์„ฑ ์ฃผ์ž… ์‹œ์Šคํ…œ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•œ)๋ณด์•ˆ ์‹œ์Šคํ…œ์€ *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*์—์„œ `str`๋กœ `token`์„ ์ œ๊ณตํ–ˆ์Šต๋‹ˆ๋‹ค:
```Python hl_lines="10"
{!../../../docs_src/security/tutorial001.py!}
```
๊ทธ๋Ÿฌ๋‚˜ ์•„์ง๋„ ์œ ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
ํ˜„์žฌ ์‚ฌ์šฉ์ž๋ฅผ ์ œ๊ณตํ•˜๋„๋ก ํ•ฉ์‹œ๋‹ค.
## ์œ ์ € ๋ชจ๋ธ ์ƒ์„ฑํ•˜๊ธฐ
๋จผ์ € Pydantic ์œ ์ € ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
Pydantic์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณธ๋ฌธ์„ ์„ ์–ธํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋‹ค๋ฅธ ๊ณณ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
=== "ํŒŒ์ด์ฌ 3.7 ์ด์ƒ"
```Python hl_lines="5 12-16"
{!> ../../../docs_src/security/tutorial002.py!}
```
=== "ํŒŒ์ด์ฌ 3.10 ์ด์ƒ"
```Python hl_lines="3 10-14"
{!> ../../../docs_src/security/tutorial002_py310.py!}
```
## `get_current_user` ์˜์กด์„ฑ ์ƒ์„ฑํ•˜๊ธฐ
์˜์กด์„ฑ `get_current_user`๋ฅผ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค.
์˜์กด์„ฑ์ด ํ•˜์œ„ ์˜์กด์„ฑ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•˜์‹ญ๋‹ˆ๊นŒ?
`get_current_user`๋Š” ์ด์ „์— ์ƒ์„ฑํ•œ ๊ฒƒ๊ณผ ๋™์ผํ•œ `oauth2_scheme`๊ณผ ์ข…์†์„ฑ์„ ๊ฐ–๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
์ด์ „์— *๊ฒฝ๋กœ ๋™์ž‘*์—์„œ ์ง์ ‘ ์ˆ˜ํ–‰ํ–ˆ๋˜ ๊ฒƒ๊ณผ ๋™์ผํ•˜๊ฒŒ ์ƒˆ ์ข…์†์„ฑ `get_current_user`๋Š” ํ•˜์œ„ ์ข…์†์„ฑ `oauth2_scheme`์—์„œ `str`๋กœ `token`์„ ์ˆ˜์‹ ํ•ฉ๋‹ˆ๋‹ค.
=== "ํŒŒ์ด์ฌ 3.7 ์ด์ƒ"
```Python hl_lines="25"
{!> ../../../docs_src/security/tutorial002.py!}
```
=== "ํŒŒ์ด์ฌ 3.10 ์ด์ƒ"
```Python hl_lines="23"
{!> ../../../docs_src/security/tutorial002_py310.py!}
```
## ์œ ์ € ๊ฐ€์ ธ์˜ค๊ธฐ
`get_current_user`๋Š” ํ† ํฐ์„ `str`๋กœ ์ทจํ•˜๊ณ  Pydantic `User` ๋ชจ๋ธ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ์šฐ๋ฆฌ๊ฐ€ ๋งŒ๋“  (๊ฐ€์งœ) ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
=== "ํŒŒ์ด์ฌ 3.7 ์ด์ƒ"
```Python hl_lines="19-22 26-27"
{!> ../../../docs_src/security/tutorial002.py!}
```
=== "ํŒŒ์ด์ฌ 3.10 ์ด์ƒ"
```Python hl_lines="17-20 24-25"
{!> ../../../docs_src/security/tutorial002_py310.py!}
```
## ํ˜„์žฌ ์œ ์ € ์ฃผ์ž…ํ•˜๊ธฐ
์ด์ œ *๊ฒฝ๋กœ ์ž‘๋™*์—์„œ `get_current_user`์™€ ๋™์ผํ•œ `Depends`๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
=== "ํŒŒ์ด์ฌ 3.7 ์ด์ƒ"
```Python hl_lines="31"
{!> ../../../docs_src/security/tutorial002.py!}
```
=== "ํŒŒ์ด์ฌ 3.10 ์ด์ƒ"
```Python hl_lines="29"
{!> ../../../docs_src/security/tutorial002_py310.py!}
```
Pydantic ๋ชจ๋ธ์ธ `User`๋กœ `current_user`์˜ ํƒ€์ž…์„ ์„ ์–ธํ•˜๋Š” ๊ฒƒ์„ ์•Œ์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.
์ด๊ฒƒ์€ ๋ชจ๋“  ์™„๋ฃŒ ๋ฐ ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์šฐ๋ฆฌ๋ฅผ ๋„์šธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
!!! ํŒ
์š”์ฒญ ๋ณธ๋ฌธ๋„ Pydantic ๋ชจ๋ธ๋กœ ์„ ์–ธ๋œ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.
์—ฌ๊ธฐ์„œ **FastAPI**๋Š” `Depends`๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ํ˜ผ๋™๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
!!! ํ™•์ธ
์ด ์˜์กด์„ฑ ์‹œ์Šคํ…œ์ด ์„ค๊ณ„๋œ ๋ฐฉ์‹์€ ๋ชจ๋‘ `User` ๋ชจ๋ธ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋‹ค์–‘ํ•œ ์˜์กด์„ฑ(๋‹ค๋ฅธ "์˜์กด์ ์ธ")์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
ํ•ด๋‹น ํƒ€์ž…์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” ์˜์กด์„ฑ์ด ํ•˜๋‚˜๋งŒ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ ์ œํ•œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
## ๋‹ค๋ฅธ ๋ชจ๋ธ
์ด์ œ *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*์—์„œ ํ˜„์žฌ ์‚ฌ์šฉ์ž๋ฅผ ์ง์ ‘ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฉฐ `Depends`๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ **์˜์กด์„ฑ ์ฃผ์ž…** ์ˆ˜์ค€์—์„œ ๋ณด์•ˆ ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋ณด์•ˆ ์š”๊ตฌ ์‚ฌํ•ญ์— ๋Œ€ํ•œ ๋ชจ๋“  ๋ชจ๋ธ ๋˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์ด ๊ฒฝ์šฐ Pydantic ๋ชจ๋ธ `User`).
๊ทธ๋Ÿฌ๋‚˜ ์ผ๋ถ€ ํŠน์ • ๋ฐ์ดํ„ฐ ๋ชจ๋ธ, ํด๋ž˜์Šค ๋˜๋Š” ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋„๋ก ์ œํ•œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๋ชจ๋ธ์— `id`์™€ `email`์ด ์žˆ๊ณ  `username`์ด ์—†๊ธธ ์›ํ•˜์‹ญ๋‹ˆ๊นŒ? ๋งž์Šต๋‹ˆ๋‹ค. ์ด๋“ค์€ ๋™์ผํ•œ ๋„๊ตฌ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
`str`๋งŒ ๊ฐ–๊ณ  ์‹ถ์Šต๋‹ˆ๊นŒ? ์•„๋‹ˆ๋ฉด ๊ทธ๋ƒฅ `dict`๋ฅผ ๊ฐ–๊ณ  ์‹ถ์Šต๋‹ˆ๊นŒ? ์•„๋‹ˆ๋ฉด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํด๋ž˜์Šค ๋ชจ๋ธ ์ธ์Šคํ„ด์Šค๋ฅผ ์ง์ ‘ ๊ฐ–๊ณ  ์‹ถ์Šต๋‹ˆ๊นŒ? ๊ทธ๋“ค์€ ๋ชจ๋‘ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
์‹ค์ œ๋กœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ๋กœ๊ทธ์ธํ•˜๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์—†์ง€๋งŒ ์•ก์„ธ์Šค ํ† ํฐ๋งŒ ์žˆ๋Š” ๋กœ๋ด‡, ๋ด‡ ๋˜๋Š” ๊ธฐํƒ€ ์‹œ์Šคํ…œ์ด ์žˆ์Šต๋‹ˆ๊นŒ? ๋‹ค์‹œ ๋งํ•˜์ง€๋งŒ ๋ชจ๋‘ ๋™์ผํ•˜๊ฒŒ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.
์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์— ํ•„์š”ํ•œ ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๋ชจ๋ธ, ๋ชจ๋“  ์ข…๋ฅ˜์˜ ํด๋ž˜์Šค, ๋ชจ๋“  ์ข…๋ฅ˜์˜ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค. **FastAPI**๋Š” ์˜์กด์„ฑ ์ฃผ์ž… ์‹œ์Šคํ…œ์„ ๋‹ค๋ฃจ์—ˆ์Šต๋‹ˆ๋‹ค.
## ์ฝ”๋“œ ์‚ฌ์ด์ฆˆ
์ด ์˜ˆ๋Š” ์žฅํ™ฉํ•ด ๋ณด์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋™์ผํ•œ ํŒŒ์ผ์—์„œ ๋ณด์•ˆ, ๋ฐ์ดํ„ฐ ๋ชจ๋ธ, ์œ ํ‹ธ๋ฆฌํ‹ฐ ๊ธฐ๋Šฅ ๋ฐ *๊ฒฝ๋กœ ์ž‘๋™*์„ ํ˜ผํ•ฉํ•˜๊ณ  ์žˆ์Œ์„ ์—ผ๋‘์— ๋‘์‹ญ์‹œ์˜ค.
๊ทธ๋Ÿฌ๋‚˜ ์ด๊ฒŒ ํ‚คํฌ์ธํŠธ์ž…๋‹ˆ๋‹ค.
๋ณด์•ˆ๊ณผ ์ข…์†์„ฑ ์ฃผ์ž… ํ•ญ๋ชฉ์„ ํ•œ ๋ฒˆ๋งŒ ์ž‘์„ฑํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์›ํ•˜๋Š” ๋งŒํผ ๋ณต์žกํ•˜๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜๋„ ์œ ์—ฐ์„ฑ๊ณผ ํ•จ๊ป˜ ํ•œ ๊ณณ์— ํ•œ ๋ฒˆ์— ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ๋™์ผํ•œ ๋ณด์•ˆ ์‹œ์Šคํ…œ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜์ฒœ ๊ฐœ์˜ ์—”๋“œํฌ์ธํŠธ(*๊ฒฝ๋กœ ์ž‘๋™*)๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๊ทธ๋“ค ๋ชจ๋‘(๋˜๋Š” ์›ํ•˜๋Š” ๋ถ€๋ถ„)๋Š” ์ด๋Ÿฌํ•œ ์˜์กด์„ฑ ๋˜๋Š” ์ƒ์„ฑํ•œ ๋‹ค๋ฅธ ์˜์กด์„ฑ์„ ์žฌ์‚ฌ์šฉํ•˜๋Š” ์ด์ ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ์ด ์ˆ˜์ฒœ ๊ฐœ์˜ *๊ฒฝ๋กœ ์ž‘๋™*์€ ๋ชจ๋‘ 3์ค„ ์ •๋„๋กœ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
=== "ํŒŒ์ด์ฌ 3.7 ์ด์ƒ"
```Python hl_lines="30-32"
{!> ../../../docs_src/security/tutorial002.py!}
```
=== "ํŒŒ์ด์ฌ 3.10 ์ด์ƒ"
```Python hl_lines="28-30"
{!> ../../../docs_src/security/tutorial002_py310.py!}
```
## ์š”์•ฝ
์ด์ œ *๊ฒฝ๋กœ ์ž‘๋™ ํ•จ์ˆ˜*์—์„œ ํ˜„์žฌ ์‚ฌ์šฉ์ž๋ฅผ ์ง์ ‘ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์šฐ๋ฆฌ๋Š” ์ด๋ฏธ ์ด๋“ค ์‚ฌ์ด์— ์žˆ์Šต๋‹ˆ๋‹ค.
์‚ฌ์šฉ์ž/ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์‹ค์ œ๋กœ `username`๊ณผ `password`๋ฅผ ๋ณด๋‚ด๋ ค๋ฉด *๊ฒฝ๋กœ ์ž‘๋™*์„ ์ถ”๊ฐ€ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
๋‹ค์Œ ์žฅ์„ ํ™•์ธํ•ด ๋ด…์‹œ๋‹ค.
Loadingโ€ฆ
Cancel
Save