【发布时间】:2015-10-22 00:19:18
【问题描述】:
我正在尝试使用基于令牌的身份验证和授权来执行登录,但由于在我的主配置文件中设置了 ApiController 操作仪表板的授权,因此我卡住了:
'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;
ApiController.php:
<?php
namespace frontend\controllers;
use Yii;
use common\models\LoginForm;
use frontend\models\ContactForm;
use yii\filters\ContentNegotiator;
use yii\web\Response;
use yii\filters\AccessControl;
use yii\rest\Controller;
use yii\filters\auth\HttpBearerAuth;
/**
* Site controller
*/
class ApiController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
'class' => HttpBearerAuth::className(),
'only' => ['dashboard'],
];
$behaviors['contentNegotiator'] = [
'class' => ContentNegotiator::className(),
'formats' => [
'application/json' => Response::FORMAT_JSON,
],
];
$behaviors['access'] = [
'class' => AccessControl::className(),
'only' => ['dashboard'],
'rules' => [
[
'actions' => ['dashboard'],
'allow' => true,
'roles' => ['@'],
],
],
];
return $behaviors;
}
public function actionLogin()
{
$model = new LoginForm();
if ($model->load(Yii::$app->getRequest()->getBodyParams(), '') && $model->login()) {
return ['access_token' => Yii::$app->user->identity->getAuthKey()];
} else {
$model->validate();
return $model;
}
}
public function actionDashboard()
{
$response = [
'username' => Yii::$app->user->identity->username,
'access_token' => Yii::$app->user->identity->getAuthKey(),
];
return $response;
}
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->getRequest()->getBodyParams(), '') && $model->validate()) {
if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
$response = [
'flash' => [
'class' => 'success',
'message' => 'Thank you for contacting us. We will respond to you as soon as possible.',
]
];
} else {
$response = [
'flash' => [
'class' => 'error',
'message' => 'There was an error sending email.',
]
];
}
return $response;
} else {
$model->validate();
return $model;
}
}
}
controller.js:
'use strict';
var controllers = angular.module('controllers', []);
controllers.controller('MainController', ['$scope', '$location', '$window',
function ($scope, $location, $window) {
$scope.loggedIn = function() {
return Boolean($window.sessionStorage.access_token);
};
$scope.logout = function () {
delete $window.sessionStorage.access_token;
$location.path('/login').replace();
};
}
]);
controllers.controller('ContactController', ['$scope', '$http', '$window',
function($scope, $http, $window) {
$scope.captchaUrl = 'site/captcha';
$scope.contact = function () {
$scope.submitted = true;
$scope.error = {};
$http.post('api/contact', $scope.contactModel).success(
function (data) {
$scope.contactModel = {};
$scope.flash = data.flash;
$window.scrollTo(0,0);
$scope.submitted = false;
$scope.captchaUrl = 'site/captcha' + '?' + new Date().getTime();
}).error(
function (data) {
angular.forEach(data, function (error) {
$scope.error[error.field] = error.message;
});
}
);
};
$scope.refreshCaptcha = function() {
$http.get('site/captcha?refresh=1').success(function(data) {
$scope.captchaUrl = data.url;
});
};
}]);
controllers.controller('DashboardController', ['$scope', '$http',
function ($scope, $http) {
$http.get('/lbp/api/dashboard').success(function (data) {
$scope.dashboard = data;
})
}
]);
controllers.controller('LoginController', ['$scope', '$http', '$window', '$location',
function($scope, $http, $window, $location) {
$scope.login = function () {
$scope.submitted = true;
$scope.error = {};
$http.post('/lbp/api/login', $scope.userModel).success(
function (data) {
$window.sessionStorage.access_token = data.access_token;
$location.path('/dashboard').replace();
}).error(
function (data) {
angular.forEach(data, function (error) {
$scope.error[error.field] = error.message;
});
}
);
};
}
]);
请为此建议我一个解决方案,我只想通过使用 rest API 来做到这一点。
【问题讨论】:
标签: angularjs api authorization yii2 token