6.8 KiB
💂♂ - 🥇 🔁
➡️ 🌈 👈 👆 ✔️ 👆 👩💻 🛠️ 🆔.
& 👆 ✔️ 🕸 ➕1️⃣ 🆔 ⚖️ 🎏 ➡ 🎏 🆔 (⚖️ 📱 🈸).
& 👆 💚 ✔️ 🌌 🕸 🔓 ⏮️ 👩💻, ⚙️ 🆔 & 🔐.
👥 💪 ⚙️ Oauth2️⃣ 🏗 👈 ⏮️ FastAPI.
✋️ ➡️ 🖊 👆 🕰 👂 🌕 📏 🔧 🔎 👈 🐥 🍖 ℹ 👆 💪.
➡️ ⚙️ 🧰 🚚 FastAPI 🍵 💂♂.
❔ ⚫️ 👀
➡️ 🥇 ⚙️ 📟 & 👀 ❔ ⚫️ 👷, & ⤴️ 👥 🔜 👟 🔙 🤔 ⚫️❔ 😥.
✍ main.py
📁 🖼 📁 main.py
:
{!../../../docs_src/security/tutorial001.py!}
🏃 ⚫️
!!! info
🥇 ❎ python-multipart
.
🤶 Ⓜ. `pip install python-multipart`.
👉 ↩️ **Oauth2️⃣** ⚙️ "📨 📊" 📨 `username` & `password`.
🏃 🖼 ⏮️:
$ uvicorn main:app --reload
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
✅ ⚫️
🚶 🎓 🩺: http://127.0.0.1:8000/docs.
👆 🔜 👀 🕳 💖 👉:

!!! check "✔ 🔼 ❗" 👆 ⏪ ✔️ ✨ 🆕 "✔" 🔼.
& 👆 *➡ 🛠️* ✔️ 🐥 🔒 🔝-▶️️ ↩ 👈 👆 💪 🖊.
& 🚥 👆 🖊 ⚫️, 👆 ✔️ 🐥 ✔ 📨 🆎 username
& password
(& 🎏 📦 🏑):

!!! note ⚫️ 🚫 🤔 ⚫️❔ 👆 🆎 📨, ⚫️ 🏆 🚫 👷. ✋️ 👥 🔜 🤚 📤.
👉 ↗️ 🚫 🕸 🏁 👩💻, ✋️ ⚫️ 👑 🏧 🧰 📄 🖥 🌐 👆 🛠️.
⚫️ 💪 ⚙️ 🕸 🏉 (👈 💪 👆).
⚫️ 💪 ⚙️ 🥉 🥳 🈸 & ⚙️.
& ⚫️ 💪 ⚙️ 👆, ℹ, ✅ & 💯 🎏 🈸.
password
💧
🔜 ➡️ 🚶 🔙 👄 & 🤔 ⚫️❔ 🌐 👈.
password
"💧" 1️⃣ 🌌 ("💧") 🔬 Oauth2️⃣, 🍵 💂♂ & 🤝.
Oauth2️⃣ 🔧 👈 👩💻 ⚖️ 🛠️ 💪 🔬 💽 👈 🔓 👩💻.
✋️ 👉 💼, 🎏 FastAPI 🈸 🔜 🍵 🛠️ & 🤝.
, ➡️ 📄 ⚫️ ⚪️➡️ 👈 📉 ☝ 🎑:
- 👩💻 🆎
username
&password
🕸, & 🎯Enter
. - 🕸 (🏃♂ 👩💻 🖥) 📨 👈
username
&password
🎯 📛 👆 🛠️ (📣 ⏮️tokenUrl="token"
). - 🛠️ ✅ 👈
username
&password
, & 📨 ⏮️ "🤝" (👥 🚫 🛠️ 🙆 👉).- "🤝" 🎻 ⏮️ 🎚 👈 👥 💪 ⚙️ ⏪ ✔ 👉 👩💻.
- 🛎, 🤝 ⚒ 🕛 ⏮️ 🕰.
- , 👩💻 🔜 ✔️ 🕹 🔄 ☝ ⏪.
- & 🚥 🤝 📎, ⚠ 🌘. ⚫️ 🚫 💖 🧲 🔑 👈 🔜 👷 ♾ (🏆 💼).
- 🕸 🏪 👈 🤝 🍕 👱.
- 👩💻 🖊 🕸 🚶 ➕1️⃣ 📄 🕸 🕸 📱.
- 🕸 💪 ☕ 🌅 💽 ⚪️➡️ 🛠️.
- ✋️ ⚫️ 💪 🤝 👈 🎯 🔗.
- , 🔓 ⏮️ 👆 🛠️, ⚫️ 📨 🎚
Authorization
⏮️ 💲Bearer
➕ 🤝. - 🚥 🤝 🔌
foobar
, 🎚Authorization
🎚 🔜:Bearer foobar
.
FastAPI'Ⓜ OAuth2PasswordBearer
FastAPI 🚚 📚 🧰, 🎏 🎚 ⚛, 🛠️ 👫 💂♂ ⚒.
👉 🖼 👥 🔜 ⚙️ Oauth2️⃣, ⏮️ 🔐 💧, ⚙️ 📨 🤝. 👥 👈 ⚙️ OAuth2PasswordBearer
🎓.
!!! info "📨" 🤝 🚫 🕴 🎛.
✋️ ⚫️ 🏆 1️⃣ 👆 ⚙️ 💼.
& ⚫️ 💪 🏆 🏆 ⚙️ 💼, 🚥 👆 Oauth2️⃣ 🕴 & 💭 ⚫️❔ ⚫️❔ 📤 ➕1️⃣ 🎛 👈 ♣ 👻 👆 💪.
👈 💼, **FastAPI** 🚚 👆 ⏮️ 🧰 🏗 ⚫️.
🕐❔ 👥 ✍ 👐 OAuth2PasswordBearer
🎓 👥 🚶♀️ tokenUrl
🔢. 👉 🔢 🔌 📛 👈 👩💻 (🕸 🏃 👩💻 🖥) 🔜 ⚙️ 📨 username
& password
✔ 🤚 🤝.
{!../../../docs_src/security/tutorial001.py!}
!!! tip
📥 tokenUrl="token"
🔗 ⚖ 📛 token
👈 👥 🚫 ✍. ⚫️ ⚖ 📛, ⚫️ 🌓 ./token
.
↩️ 👥 ⚙️ ⚖ 📛, 🚥 👆 🛠️ 🔎 `https://example.com/`, ⤴️ ⚫️ 🔜 🔗 `https://example.com/token`. ✋️ 🚥 👆 🛠️ 🔎 `https://example.com/api/v1/`, ⤴️ ⚫️ 🔜 🔗 `https://example.com/api/v1/token`.
⚙️ ⚖ 📛 ⚠ ⚒ 💭 👆 🈸 🚧 👷 🏧 ⚙️ 💼 💖 [⛅ 🗳](../../advanced/behind-a-proxy.md){.internal-link target=_blank}.
👉 🔢 🚫 ✍ 👈 🔗 / ➡ 🛠️, ✋️ 📣 👈 📛 /token
🔜 1️⃣ 👈 👩💻 🔜 ⚙️ 🤚 🤝. 👈 ℹ ⚙️ 🗄, & ⤴️ 🎓 🛠️ 🧾 ⚙️.
👥 🔜 🔜 ✍ ☑ ➡ 🛠️.
!!! info
🚥 👆 📶 ⚠ "✍" 👆 💪 👎 👗 🔢 📛 tokenUrl
↩️ token_url
.
👈 ↩️ ⚫️ ⚙️ 🎏 📛 🗄 🔌. 👈 🚥 👆 💪 🔬 🌅 🔃 🙆 👫 💂♂ ⚖ 👆 💪 📁 & 📋 ⚫️ 🔎 🌖 ℹ 🔃 ⚫️.
oauth2_scheme
🔢 👐 OAuth2PasswordBearer
, ✋️ ⚫️ "🇧🇲".
⚫️ 💪 🤙:
oauth2_scheme(some, parameters)
, ⚫️ 💪 ⚙️ ⏮️ Depends
.
⚙️ ⚫️
🔜 👆 💪 🚶♀️ 👈 oauth2_scheme
🔗 ⏮️ Depends
.
{!../../../docs_src/security/tutorial001.py!}
👉 🔗 🔜 🚚 str
👈 🛠️ 🔢 token
➡ 🛠️ 🔢.
FastAPI 🔜 💭 👈 ⚫️ 💪 ⚙️ 👉 🔗 🔬 "💂♂ ⚖" 🗄 🔗 (& 🏧 🛠️ 🩺).
!!! info "📡 ℹ"
FastAPI 🔜 💭 👈 ⚫️ 💪 ⚙️ 🎓 OAuth2PasswordBearer
(📣 🔗) 🔬 💂♂ ⚖ 🗄 ↩️ ⚫️ 😖 ⚪️➡️ fastapi.security.oauth2.OAuth2
, ❔ 🔄 😖 ⚪️➡️ fastapi.security.base.SecurityBase
.
🌐 💂♂ 🚙 👈 🛠️ ⏮️ 🗄 (& 🏧 🛠️ 🩺) 😖 ⚪️➡️ `SecurityBase`, 👈 ❔ **FastAPI** 💪 💭 ❔ 🛠️ 👫 🗄.
⚫️❔ ⚫️ 🔨
⚫️ 🔜 🚶 & 👀 📨 👈 Authorization
🎚, ✅ 🚥 💲 Bearer
➕ 🤝, & 🔜 📨 🤝 str
.
🚥 ⚫️ 🚫 👀 Authorization
🎚, ⚖️ 💲 🚫 ✔️ Bearer
🤝, ⚫️ 🔜 📨 ⏮️ 4️⃣0️⃣1️⃣ 👔 📟 ❌ (UNAUTHORIZED
) 🔗.
👆 🚫 ✔️ ✅ 🚥 🤝 🔀 📨 ❌. 👆 💪 💭 👈 🚥 👆 🔢 🛠️, ⚫️ 🔜 ✔️ str
👈 🤝.
👆 💪 🔄 ⚫️ ⏪ 🎓 🩺:

👥 🚫 ✔ 🔬 🤝, ✋️ 👈 ▶️ ⏪.
🌃
, 3️⃣ ⚖️ 4️⃣ ➕ ⏸, 👆 ⏪ ✔️ 🐒 📨 💂♂.