fastapi/docs/em/docs/tutorial/security/first-steps.md

6.8 KiB
Raw Blame History

💂‍♂ - 🥇 🔁

➡️ 🌈 👈 👆 ✔️ 👆 👩‍💻 🛠️ 🆔.

& 👆 ✔️ 🕸 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.

⚙️📛 ⚠ ⚒ 💭 👆 🈸 🚧 👷 🏧 ⚙️ 💼 💖 🗳{.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 🤝, 🔜 📨 ⏮️ 401 👔 📟 (UNAUTHORIZED) 🔗.

👆 🚫 ✔️ 🚥 🤝 🔀 📨 . 👆 💪 💭 👈 🚥 👆 🔢 🛠️, 🔜 ✔️ str 👈 🤝.

👆 💪 🔄 🎓 🩺:

👥 🚫🔬 🤝, 👈 ▶️ .

🌃

, 3 ⚖️ 4 ⏸, 👆 ✔️ 🐒 📨 💂‍♂.