kanganxi
V2EX  ›  PHP

Yii2 RESTful Web 服务 授权认证和 Ajax 跨域配置问题

  •  
  •   kanganxi · Sep 29, 2017 · 5047 views
    This topic created in 3226 days ago, the information mentioned may be changed or developed.

    开发 api 接口,做了 HttpBearerAuth 授权登陆之后,ajax 请求跨域,我的配置如下:

    控制器配置如下:

     public function behaviors()
            {
                return ArrayHelper::merge(parent::behaviors(), [
                // 授权验证
                'authenticator' => [
                    'class' => HttpBearerAuth::className(),
                    'optional' => [
                        'login',
                        'signup',
                    ],
                ],
                    'corsFilter'  => [
                        'class' => \yii\filters\Cors::className(),
                        'cors'  => [
                            'Origin' => ['*'],
                            'Access-Control-Request-Headers' => ['authorization'],
                        ],
                    ],
            ]);
            }
    

    目前是只要需要登陆授权的请求,通过 ajax 请求都会出现跨域,并提示 cors 预检未通过,我的通过请求头 Authorization 来传递 token ;授权采用的是 HttpBearerAuth 方式

    请知道的 V 友麻烦回答一下,小弟感激不尽,

    2 replies    2017-09-30 15:19:00 +08:00
    alinwu05
        1
    alinwu05  
       Sep 30, 2017   ❤️ 1
    其实就是 Yii2 怎么处理跨域的问题。我写过一篇文章关于这个,有空看看就懂了: https://my.oschina.net/u/248080/blog/875243
    kanganxi
        2
    kanganxi  
    OP
       Sep 30, 2017
    @alinwu05 非常感谢您的回复,看到你的文章之后豁然开朗了,
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1062 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 18:33 · PVG 02:33 · LAX 11:33 · JFK 14:33
    ♥ Do have faith in what you're doing.