<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [入門(Getting Started)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-2)[應用結構(Application Structure)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-3)[請求處理(Handling Requests)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-4)[關鍵概念(Key Concepts)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-5)[配合數據庫工作(Working with Databases)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-6)[接收用戶數據(Getting Data from Users)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-7)[顯示數據(Displaying Data)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-8)[安全(Security)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-9)[緩存(Caching)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-10)[RESTful Web 服務?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-11) [快速入門(Quick Start)](http://www.yiichina.com/doc/guide/2.0/rest-quick-start)[資源(Resources)](http://www.yiichina.com/doc/guide/2.0/rest-resources)[控制器(Controllers)](http://www.yiichina.com/doc/guide/2.0/rest-controllers)[路由(Routing)](http://www.yiichina.com/doc/guide/2.0/rest-routing)[格式化響應(Response Formatting)](http://www.yiichina.com/doc/guide/2.0/rest-response-formatting)[授權驗證(Authentication)](http://www.yiichina.com/doc/guide/2.0/rest-authentication)[速率限制(Rate Limiting)](http://www.yiichina.com/doc/guide/2.0/rest-rate-limiting)[版本化(Versioning)](http://www.yiichina.com/doc/guide/2.0/rest-versioning)[錯誤處理(Error Handling)](http://www.yiichina.com/doc/guide/2.0/rest-error-handling) [開發工具(Development Tools)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-12)[測試(Testing)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-13)[高級專題(Special Topics)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-14)[小部件(Widgets)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-15)[助手類(Helpers)?](http://www.yiichina.com/doc/guide/2.0/rest-authentication#w0-16) # 認證 和Web應用不同,RESTful APIs 通常是無狀態的,也就意味著不應使用sessions 或 cookies, 因此每個請求應附帶某種授權憑證,因為用戶授權狀態可能沒通過sessions 或 cookies維護, 常用的做法是每個請求都發送一個秘密的access token來認證用戶,由于access token可以唯一識別和認證用戶,?**API 請求應通過HTTPS來防止man-in-the-middle (MitM) 中間人攻擊**. 下面有幾種方式來發送access token: * [HTTP 基本認證](http://en.wikipedia.org/wiki/Basic_access_authentication): access token 當作用戶名發送,應用在access token可安全存在API使用端的場景,例如,API使用端是運行在一臺服務器上的程序。 * 請求參數: access token 當作API URL請求參數發送,例如?`https://example.com/users?access-token=xxxxxxxx`,由于大多數服務器都會保存請求參數到日志, 這種方式應主要用于`JSONP`?請求,因為它不能使用HTTP頭來發送access token * [OAuth 2](http://oauth.net/2/): 使用者從認證服務器上獲取基于OAuth2協議的access token,然后通過?[HTTP Bearer Tokens](http://tools.ietf.org/html/rfc6750)?發送到API 服務器。 Yii 支持上述的認證方式,你也可很方便的創建新的認證方式。 為你的APIs啟用認證,做以下步驟: 1. 配置`user`?應用組件: * 設置 yii\web\User::enableSession 屬性為?`false`. * 設置 yii\web\User::loginUrl 屬性為`null`?顯示一個HTTP 403 錯誤而不是跳轉到登錄界面. 2. 在你的REST 控制器類中配置`authenticator`?行為來指定使用哪種認證方式 3. 在你的yii\web\User::identityClass 類中實現 yii\web\IdentityInterface::findIdentityByAccessToken() 方法. 步驟1不是必要的,但是推薦配置,因為RESTful APIs應為無狀態的,當yii\web\User::enableSession為false, 請求中的用戶認證狀態就不能通過session來保持,每個請求的認證通過步驟2和3來實現。 > 提示: 如果你將RESTful APIs作為應用開發,可以設置應用配置中?`user`?組件的yii\web\User::enableSession, 如果將RESTful APIs作為模塊開發,可以在模塊的?`init()`?方法中增加如下代碼,如下所示: ~~~ public function init() { parent::init(); \Yii::$app->user->enableSession = false; } ~~~ 例如,為使用HTTP Basic Auth,可配置`authenticator`?行為,如下所示: ~~~ use yii\filters\auth\HttpBasicAuth; public function behaviors() { $behaviors = parent::behaviors(); $behaviors['authenticator'] = [ 'class' => HttpBasicAuth::className(), ]; return $behaviors; } ~~~ 如果你系那個支持以上3個認證方式,可以使用`CompositeAuth`,如下所示: ~~~ use yii\filters\auth\CompositeAuth; use yii\filters\auth\HttpBasicAuth; use yii\filters\auth\HttpBearerAuth; use yii\filters\auth\QueryParamAuth; public function behaviors() { $behaviors = parent::behaviors(); $behaviors['authenticator'] = [ 'class' => CompositeAuth::className(), 'authMethods' => [ HttpBasicAuth::className(), HttpBearerAuth::className(), QueryParamAuth::className(), ], ]; return $behaviors; } ~~~ `authMethods`?中每個單元應為一個認證方法名或配置數組。 `findIdentityByAccessToken()`方法的實現是系統定義的, 例如,一個簡單的場景,當每個用戶只有一個access token, 可存儲access token 到user表的`access_token`列中, 方法可在`User`類中簡單實現,如下所示: ~~~ use yii\db\ActiveRecord; use yii\web\IdentityInterface; class User extends ActiveRecord implements IdentityInterface { public static function findIdentityByAccessToken($token, $type = null) { return static::findOne(['access_token' => $token]); } } ~~~ 在上述認證啟用后,對于每個API請求,請求控制器都會在它的`beforeAction()`步驟中對用戶進行認證。 如果認證成功,控制器再執行其他檢查(如頻率限制,操作權限),然后再執行操作, 授權用戶信息可使用`Yii::$app->user->identity`獲取. 如果認證失敗,會發送一個HTTP狀態碼為401的響應,并帶有其他相關信息頭(如HTTP 基本認證會有`WWW-Authenticate`?頭信息). ## 授權 在用戶認證成功后,你可能想要檢查他是否有權限執行對應的操作來獲取資源,這個過程稱為?*authorization*?, 詳情請參考[Authorization section](http://www.yiichina.com/doc/guide/2.0/security-authorization). 如果你的控制器從yii\rest\ActiveController類繼承,可覆蓋 yii\rest\Controller::checkAccess() 方法 來執行授權檢查,該方法會被yii\rest\ActiveController內置的操作調用。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看