Yii2 CORS with Auth not working for non CRUD actions

前端 未结 2 1975
耶瑟儿~
耶瑟儿~ 2021-01-13 02:03

I am building an API in Yii2 and have added CORS and authentication. This works fine for all Create/Read/Update/Delete actions but not for custom actions. Has anyone experie

2条回答
  •  臣服心动
    2021-01-13 02:53

    Just move your corsFilter block above the authenticator block like this:

    public function behaviors()
        {
            return [
                'corsFilter' => [
                    'class' => \yii\filters\Cors::className(),
                ],
                'authenticator' => [
                    'class' => HttpBearerAuth::className(),
                ],
                'contentNegotiator' => [
                    'class' => ContentNegotiator::className(),
                    'formats' => [
                        'application/json' => Response::FORMAT_JSON,
                    ],
                ],
    
            ];
        }
    

提交回复
热议问题