rowasc / yii2oauthserver
一个实现TheLeague OAuth组件的Yii2模块
Requires
- league/oauth2-server: @stable
- rowasc/yii2loggingutils: *
- yiisoft/yii2: @stable
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-28 17:49:19 UTC
README
-
将迁移文件复制到您自己的迁移目录(在您的yii2项目内部),并运行迁移。确保在项目中使用之前更新client_id和client_secret,因为它们在迁移中添加的方式并不安全!
-
编辑您的yii2配置,在"components"下包含以下内容:
'ResourceServerComponent'=> [ 'class' => 'rowasc\yii2oauthserver\components\ResourceServerComponent' ], 'AuthServerComponent'=> [ 'class' => 'rowasc\yii2oauthserver\components\AuthServerComponent' ],
- 编辑您的yii2配置,在"modules"下包含以下内容:
'yii2oauthserver' => [ 'class' => '@vendor\rowasc\yii2oauthserver\Module', ]
- 在您的 .htaccess 文件中,允许Authorization头通过,因为如果没有这行,Authorization头通常会被清除
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
-
让您的base api控制器继承" \rowasc\yii2oauthserver\controllers\ApiController" .
-
创建一个AuthorizationController,并在其中继承 \rowasc\yii2oauthserver\controllers\AuthorizationController。
-
在您的User模型中,扩展rowasc\yii2oauthserver\models\User。
这将创建一个"/authorization/login"和"/authorization/logout"端点,允许您为API身份验证获取和过期载体令牌。
示例:获取新的载体令牌
POST /v1/authorization/login HTTP/1.1 Host: api.yii2-starter-kit.dev Content-Type: application/json Cache-Control: no-cache Postman-Token: 7dd28588-b30d-a252-5c7e-5ecc9d1ab740
{ "client_id": "client_name", "client_secret": "client_secret", "username": "webmaster", "password": "webmaster" }
响应
{ "access_token": "ENMTcmTSgQTmwCpVbaO3AHHbhbJYgziiZzjWzWUd", "token_type": "Bearer", "expires_in": 43200 }
示例:使令牌失效,这样用户就无法访问API
POST /v1/authorization/logout HTTP/1.1 Host: api.yii2-starter-kit.dev Content-Type: application/json Authorization: Bearer ENMTcmTSgQTmwCpVbaO3AHHbhbJYgziiZzjWzWUd Cache-Control: no-cache Postman-Token: 9b86bad2-dd05-898b-5888-6047e0dd2772
{ "client_id": "client_name", "client_secret": "client_secret", "username": "webmaster", "password": "webmaster" }
响应
{ "status": true }