Skip to content

Commit

Permalink
🎨 [pre-commit.ci] Auto format from pre-commit.com hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Aug 18, 2022
1 parent 2a9f100 commit 1959b07
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions docs/zh/docs/tutorial/security/first-steps.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
!!! info "说明"

先安装 <a href="https://andrew-d.github.io/python-multipart/" class="external-link" target="_blank">`python-multipart`</a>。

安装命令: `pip install python-multipart`。

这是因为 **OAuth2** 使用**表单数据**发送 `username` 与 `password`。

用下面的命令运行该示例:
Expand All @@ -57,7 +57,7 @@ $ uvicorn main:app --reload
!!! check "Authorize 按钮!"

页面右上角出现了一个「**Authorize**」按钮。

*路径操作*的右上角也出现了一个可以点击的小锁图标。

点击 **Authorize** 按钮,弹出授权表单,输入 `username``password` 及其它可选字段:
Expand Down Expand Up @@ -111,11 +111,11 @@ OAuth2 的设计目标是为了让后端或 API 独立于服务器验证用户
!!! info "说明"

`Beare` 令牌不是唯一的选择。

但它是最适合这个用例的方案。

甚至可以说,它是适用于绝大多数用例的最佳方案,除非您是 OAuth2 的专家,知道为什么其它方案更合适。

本例中,**FastAPI** 还提供了构建工具。

创建 `OAuth2PasswordBearer` 的类实例时,要传递 `tokenUrl` 参数。该参数包含客户端(用户浏览器中运行的前端) 的 URL,用于发送 `username``password`,并获取令牌。
Expand All @@ -127,9 +127,9 @@ OAuth2 的设计目标是为了让后端或 API 独立于服务器验证用户
!!! tip "提示"

在此,`tokenUrl="token"` 指向的是暂未创建的相对 URL `token`。这个相对 URL 相当于 `./token`。

因为使用的是相对 URL,如果 API 位于 `https://example.com/`,则指向 `https://example.com/token`。但如果 API 位于 `https://example.com/api/v1/`,它指向的就是`https://example.com/api/v1/token`。

使用相对 URL 非常重要,可以确保应用在遇到[使用代理](../../advanced/behind-a-proxy.md){.internal-link target=_blank}这样的高级用例时,也能正常运行。

该参数不会创建端点或*路径操作*,但会声明客户端用来获取令牌的 URL `/token` 。此信息用于 OpenAPI 及 API 文档。
Expand All @@ -139,7 +139,7 @@ OAuth2 的设计目标是为了让后端或 API 独立于服务器验证用户
!!! info "说明"

严苛的 **Pythonista** 可能不喜欢用 `tokenUrl` 这种命名风格代替 `token_url`。

这种命名方式是因为要使用与 OpenAPI 规范中相同的名字。以便在深入校验安全方案时,能通过复制粘贴查找更多相关信息。

`oauth2_scheme` 变量是 `OAuth2PasswordBearer` 的实例,也是**可调用项**
Expand Down Expand Up @@ -167,7 +167,7 @@ oauth2_scheme(some, parameters)
!!! info "技术细节"

**FastAPI** 使用(在依赖项中声明的)类 `OAuth2PasswordBearer` 在 OpenAPI 中定义安全方案,这是因为它继承自 `fastapi.security.oauth2.OAuth2`,而该类又是继承自`fastapi.security.base.SecurityBase`。

所有与 OpenAPI(及 API 文档)集成的安全工具都继承自 `SecurityBase`, 这就是为什么 **FastAPI** 能把它们集成至 OpenAPI 的原因。

## 实现的操作
Expand Down

0 comments on commit 1959b07

Please sign in to comment.