TP5和YII防止跨域的方法

TP5:

  /*防止跨域*/      
        header('Access-Control-Allow-Origin: '.$_SERVER['HTTP_ORIGIN']);
        header('Access-Control-Allow-Credentials: true');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept, authKey, sessionId");   

YII2:

  public function behaviors()
    {
          return ArrayHelper::merge([
                [
                        'class' => Cors::className(),
                        'cors' => [
                            'Origin' => ['*'],
                            'Access-Control-Request-Method' => ['GET','POST','DELETE','OPTIONS','PUT'],
                            'Access-Control-Request-Headers'=>['*']
                        ],

                ],
                //權限認證器
                'authenticator' =>[
                            'class' => CompositeAuth::className(),
                            'authMethods' => [
                                HttpBearerAuth::className(),
                            ],
                            'optional'  => [ 'login','authlogin','codelogin'],

                ]
        ], parent::behaviors());
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章