Angularjs 使用oAuth2协议(HttpBearerAuth)通过angular和yii2进行基于令牌的身份验证和授权
我试图使用基于令牌的身份验证和授权来执行登录,但在ApiController的操作仪表板上的授权被卡住了,因为在我的主配置文件中,我设置了:Angularjs 使用oAuth2协议(HttpBearerAuth)通过angular和yii2进行基于令牌的身份验证和授权,angularjs,api,authorization,yii2,token,Angularjs,Api,Authorization,Yii2,Token,我试图使用基于令牌的身份验证和授权来执行登录,但在ApiController的操作仪表板上的授权被卡住了,因为在我的主配置文件中,我设置了: 'enableSession' => false, 在ApiController中,我有: public function actionDashboard() { $response = [ 'username' => Yii::$app->user->identity->us
'enableSession' => false,
在ApiController中,我有:
public function actionDashboard()
{
$response = [
'username' => Yii::$app->user->identity->username,
'access_token' => Yii::$app->user->identity->getAuthKey(),
];
return $response;
}
我无法正确使用令牌执行授权过程。这是我的完整代码
main-local.php:
<?php
use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());
$config = [
'language' => 'en-US',
'name'=>'Lead Battery Recycling',
'components' => [
'user' => [
'identityClass' => 'common\models\User',
'enableSession' => false,
'loginUrl' => null,
'enableAutoLogin' => false,
'identityCookie' => [
'name' => '_frontendUser', // unique for backend
'path'=>'/gravita_lbp/frontend/web' // correct path for the backend app.
]
],
'session' => [
'name' => 'PHPFRONTSESSID',
'savePath' => __DIR__ . '/../tmp',
],
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => '7ZO7uJfychV7ozQOPnfQGS6zdXLz9Lx0',
'baseUrl' => $baseUrl,
'parsers' => [
'application/json' => 'yii\web\JsonParser',
]
],
'urlManager' => [
'class' => 'yii\web\UrlManager',
'baseUrl' => $baseUrl,
'enablePrettyUrl' => true,
'showScriptName' => false,
//'enableStrictParsing' => true,
'rules' => [
['class' => 'yii\rest\UrlRule', 'controller' => 'api'],
],
],
'urlManagerBackEnd' => [
'class' => 'yii\web\urlManager',
'baseUrl' => '/gravita_lbp/backend/web/',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
],
'as access' => [
'class' => 'mdm\admin\components\AccessControl',
'allowActions' => [
'site/*',
'api/login',
'api/dashboard' // add or remove allowed actions to this list
]
],
];
if (!YII_ENV_TEST) {
// configuration adjustments for 'dev' environment
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = 'yii\debug\Module';
$config['bootstrap'][] = 'gii';
$config['modules']['gii'] = 'yii\gii\Module';
}
return $config;
请为我提供一个解决方案,我只想通过使用rest API来实现这一点。因为rest API是无状态的, 因此,您应该将会话设置为false。 然后,你不需要把两个不同的问题混在一起 开发人员身份验证 最终用户身份验证 通常,您应该首先对开发人员进行身份验证 在允许他们使用API之前 基本凭证或不记名代币。 之后,您应该将资源公开为端点 开发人员可以查询的。这是你可以考虑打破的
仪表板向下延伸到可以单独查询的组件。我的文章迟了,但可能对未来的读者有所帮助 您需要在每个请求中发送访问令牌。假设创建名为auth.js的简单工厂
app.factory('authInterceptor', function($q, $window, $rootScope) {
return {
request: function(config) {
if ($window.sessionStorage.access_token) {
//HttpBearerAuth
config.headers.Authorization = 'Bearer ' + $window.sessionStorage.access_token;
}
return config;
},
}
});
加
$httpProvider.interceptors.push('authInterceptor');
在angular.module内,可帮助您添加
Authorization:Bearer your_access_token
在请求头中。并由您的Yii控制器行为用于验证您的用户
$httpProvider.interceptors.push('authInterceptor');
Authorization:Bearer your_access_token