如何使用Yii2在localhost中启动OAuth2.0服务器?

时间:2018-03-01 13:46:29

标签: oauth-2.0 yii2 yii2-advanced-app

我是OAuth2.0和YII2的新手。我正在尝试使用Filsh Library设置OAuth。根据文档正确设置一切。但是在尝试点击网址时,它会抛出我找不到的404页面。请帮助我,我错了,以及如何启动服务器。

项目文件结构

Project File Structure

Step 1: Composer.json

  "filsh/yii2-oauth2-server": "~2.0"

Step 2: module

'oauth2' => [
        'class' => 'filsh\yii2\oauth2server\Module',
        'tokenParamName' => 'access_token',
        'tokenAccessLifetime' => 3600 * 24,
        'storageMap' => [
            'user_credentials' => 'api\models\Users',
        ],
        'grantTypes' => [
            'user_credentials' => [
                'class' => 'OAuth2\GrantType\UserCredentials',
            ],
            'refresh_token' => [
                'class' => 'OAuth2\GrantType\RefreshToken',
                'always_issue_new_refresh_token' => true
            ]
        ]
    ]

Step 3: Url

 'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            'POST oauth2/<action:\w+>' => 'oauth2/rest/<action>',
        ],

Step 4: AuthController

class AuthController extends ActiveController
{
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' => CompositeAuth::className(),
                'authMethods' => [
                    ['class' => HttpBearerAuth::className()],
                    ['class' => QueryParamAuth::className(), 'tokenParam' => 'access_token'],
                ]
            ],
            'exceptionFilter' => [
                'class' => ErrorToExceptionFilter::className()
            ],
        ]);
    }

} 

Step 5: Implement IdentityInterface, UserCredentialsInterface

   class Users extends api\models\Users implements \OAuth2\Storage\UserCredentialsInterface, IdentityInterface
    { 

    }

尝试点击http://192.168.100.3/dashboard/oauth2/token 它会抛出消息

404 Not Found http://192.168.100.3/dashboard/oauth2/token 

1 个答案:

答案 0 :(得分:0)

似乎您正在使用的仓库不再维护,请参阅此ISSUE,因此没有使用集成其所有者已放弃并因为过去1年内未更新代码而未激活的库。< / p>

我建议您使用此回购borodulin/yii2-oauth2-server的替代方案,因为有些人正在尝试实施相同的解决方案,而且他们正在从Flish转移到我在此处提到的替代回购ISSUE