mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 05:48:11 +08:00
Update rest-authentication.md
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
因此每个请求应附带某种授权凭证,因为用户授权状态可能没通过 sessions 或 cookies 维护,
|
||||
常用的做法是每个请求都发送一个秘密的 access token来认证用户,
|
||||
由于 access token 可以唯一识别和认证用户,
|
||||
**API 请求应通过HTTPS来防止man-in-the-middle (MitM) 中间人攻击**.
|
||||
**API 请求应通过 HTTPS 来防止 man-in-the-middle (MitM) 中间人攻击**。
|
||||
|
||||
下面有几种方式来发送access token:
|
||||
|
||||
@ -63,7 +63,7 @@ public function behaviors()
|
||||
}
|
||||
```
|
||||
|
||||
如果你系那个支持以上3个认证方式,可以使用`CompositeAuth`,如下所示:
|
||||
如果你想支持上面解释的所有三种认证方法,可以使用 `CompositeAuth`,如下所示:
|
||||
|
||||
```php
|
||||
use yii\filters\auth\CompositeAuth;
|
||||
@ -90,7 +90,7 @@ public function behaviors()
|
||||
|
||||
|
||||
`findIdentityByAccessToken()`方法的实现是系统定义的,
|
||||
例如,一个简单的场景,当每个用户只有一个access token, 可存储access token 到user表的`access_token`列中,
|
||||
例如,一个简单的场景,当每个用户只有一个 access token,可存储 access token 到 user 表的 `access_token` 列中,
|
||||
方法可在 `User` 类中简单实现,如下所示:
|
||||
|
||||
```php
|
||||
@ -113,7 +113,7 @@ class User extends ActiveRecord implements IdentityInterface
|
||||
授权用户信息可使用 `Yii::$app->user->identity` 获取.
|
||||
|
||||
如果认证失败,会发送一个 HTTP 状态码为 401 的响应,
|
||||
并带有其他相关信息头(如HTTP 基本认证会有`WWW-Authenticate` 头信息).
|
||||
并带有其他相关信息头(如HTTP 基本认证会有 `WWW-Authenticate` 头信息)。
|
||||
|
||||
|
||||
## 授权 <a name="authorization"></a>
|
||||
|
||||
Reference in New Issue
Block a user