Yii2 Rest API Bearer Authentication

前端 未结 1 501
自闭症患者
自闭症患者 2021-01-04 11:43

I\'ve made a Yii2 REST API. With the API you can get a list of cars. Now I want to use the Bearer Authentication to protect the API. But I don\'t know how it works.

相关标签:
1条回答
  • 2021-01-04 12:10

    \yii\filters\auth\HttpBearerAuth::authenticate() will simply call \yii\web\User::loginByAccessToken() :

    $class = $this->identityClass;
    $identity = $class::findIdentityByAccessToken($token, $type);
    

    So you just need to implement findIdentityByAccessToken() in your user identity class, e.g. :

    public static function findIdentityByAccessToken($token, $type = null)
    {
        return static::findOne(['auth_key' => $token]);
    }
    
    0 讨论(0)
提交回复
热议问题