Yii2+;restapi+;cors+;承载-创建自定义控制器操作时未经授权
我正在制作angular 2/Yii2应用程序 当我使用承载头从Postman调用自定义控制器/操作时,一切正常,但当我使用angular 2应用程序(localhost:4200)的相同头调用它时,我总是得到一个错误。 当我执行标准操作时,例如: 一切都很好,只有当我创建自定义操作时,我才会得到授权 这是跨域应用程序, angular可在web.example.com上获得, yii2应用程序位于srv.example.com上Yii2+;restapi+;cors+;承载-创建自定义控制器操作时未经授权,rest,yii2,cors,Rest,Yii2,Cors,我正在制作angular 2/Yii2应用程序 当我使用承载头从Postman调用自定义控制器/操作时,一切正常,但当我使用angular 2应用程序(localhost:4200)的相同头调用它时,我总是得到一个错误。 当我执行标准操作时,例如: 一切都很好,只有当我创建自定义操作时,我才会得到授权 这是跨域应用程序, angular可在web.example.com上获得, yii2应用程序位于srv.example.com上 namespace app\controllers\restap
namespace app\controllers\restapi;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;
class SearchOrderController extends \yii\rest\Controller
{
public $modelClass = 'app\models\Order';
public function behaviors()
{
$behaviors = parent::behaviors();
$auth = $behaviors['authenticator'];
unset($behaviors['authenticator']);
$behaviors['corsFilter'] = [
'class' => \yii\filters\Cors::className(),
];
$behaviors['authenticator'] = [
'class' => CompositeAuth::className(),
'authMethods' => [
HttpBearerAuth::className(),
],
];
// avoid authentication on CORS-pre-flight requests (HTTP OPTIONS method)
return $behaviors;
}
public function actionSearch(){
\Yii::$app->response->format=\yii\web\Response::FORMAT_JSON;
return ['index'=>'some text', 'value'=>123];
}
}
还有我的url管理器:
[
'class' => 'yii\rest\UrlRule',
'controller' => 'order',
'pluralize' => false,
'extraPatterns' => [
'GET order/<id:\id+>' => 'order/view',
]
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'search-order',
'pluralize' => false,
'extraPatterns' => [
'GET search-order/search' => 'search-order/search',
]
[
'class'=>'yii\rest\UrlRule',
“控制器”=>“订单”,
“多元化”=>错误,
“外部模式”=>[
“获取订单/”=>“订单/视图”,
]
],
[
'class'=>'yii\rest\UrlRule',
“控制器”=>“搜索顺序”,
“多元化”=>错误,
“外部模式”=>[
'获取搜索顺序/搜索'=>'搜索顺序/搜索',
]
我已使用此处发布的解决方案解决了我的问题:
和实现自定义Cors类:
<?php
namespace app\components;
use Yii;
use yii\filters\Cors;
class CustomCors extends Cors
{
public function beforeAction($action)
{
parent::beforeAction($action);
if (Yii::$app->getRequest()->getMethod() === 'OPTIONS') {
Yii::$app->getResponse()->getHeaders()->set('Allow', 'POST GET PUT');
Yii::$app->end();
}
return true;
}
}
我已使用此处发布的解决方案解决了我的问题: 和实现自定义Cors类:
<?php
namespace app\components;
use Yii;
use yii\filters\Cors;
class CustomCors extends Cors
{
public function beforeAction($action)
{
parent::beforeAction($action);
if (Yii::$app->getRequest()->getMethod() === 'OPTIONS') {
Yii::$app->getResponse()->getHeaders()->set('Allow', 'POST GET PUT');
Yii::$app->end();
}
return true;
}
}
看起来这是个问题。我认为有两件事需要解决
首先,在扩展yii\rest\Controller
时,您需要实现一个操作来响应该请求。这样的操作是在其子控制器yii\rest\ActiveController
中执行的。因此,您可以使用操作()
函数导入它,就像它完成了一样,或者您可以直接实现它,就像在另一篇satck overflow文章中一样:
然后您需要定义它们的相关规则:
[
'class' => 'yii\rest\UrlRule',
'controller' => 'order',
'pluralize' => false,
'extraPatterns' => [
'GET order/<id:\id+>' => 'order/view',
'OPTIONS order/<id:\id+>' => 'order/options',
]
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'search-order',
'pluralize' => false,
'extraPatterns' => [
'GET search-order/search' => 'search-order/search',
'OPTIONS search-order/search' => 'search-order/options',
]
看起来这是个问题。我认为有两件事需要解决
首先,在扩展yii\rest\Controller
时,您需要实现一个操作来响应该请求。这样的操作是在其子控制器yii\rest\ActiveController
中执行的。因此,您可以使用操作()
函数导入它,就像它完成了一样,或者您可以直接实现它,就像在另一篇satck overflow文章中一样:
然后您需要定义它们的相关规则:
[
'class' => 'yii\rest\UrlRule',
'controller' => 'order',
'pluralize' => false,
'extraPatterns' => [
'GET order/<id:\id+>' => 'order/view',
'OPTIONS order/<id:\id+>' => 'order/options',
]
],
[
'class' => 'yii\rest\UrlRule',
'controller' => 'search-order',
'pluralize' => false,
'extraPatterns' => [
'GET search-order/search' => 'search-order/search',
'OPTIONS search-order/search' => 'search-order/options',
]
你确定GET和POST是失败的请求吗?请检查你浏览器的网络选项卡,看看它是否是
OPTIONS/users
失败的请求?它是OPTIONS…因为我收到的消息是:OPTIONS 401(Unauthorized)我能做些什么呢?这是针对这个问题的。你确定得到并发布的是失败的请求吗?你能检查一下浏览器的网络选项卡,看看它是否不是一个OPTIONS/用户失败的请求吗?这是OPTIONS…我得到的信息是:OPTIONS 401(未经授权)我能用它做什么吗?这是针对这个问题的
// avoid authentication on CORS-pre-flight requests (HTTP OPTIONS method)
$behaviors['authenticator']['except'] = ['options'];