<span style="font-size:18px;"> function behaviors() {
$behaviors = parent::behaviors();
// 当前操作的id
$currentAction = Yii::$app->controller->action->id;
// 需要进行认证的action
$authActions = ['index'];
// 需要进行认证的action就要设置安全认证类
if(in_array($currentAction, $authActions)) {
$behaviors['authenticator'] = [
'class' => QueryParamAuth::className(),
];
}
return ArrayHelper::merge([
//设置可以接收访问的域和方法。
[
'class' => Cors::className(),
'cors' => [
'Origin' => ['*'],
// 'Access-Control-Request-Method' => ['GET', 'HEAD', 'OPTIONS'],
'Access-Control-Request-Headers' => ['Origin', 'X-Requested-With', 'Content-Type', 'Accept'],
'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
],
],
], $behaviors);
}</span>
yii2 restful同一个控制器设置允许认证不允许认证方法,跨域设置
最新推荐文章于 2024-01-18 15:03:49 发布