在ZF2控制器获取请求授权报头(Getting request Authorization head

2019-08-21 17:57发布

我使用ZF2和出于某种原因,我可以得到所有我送除Authorization头标题 - 就像它过滤掉。

我试图让在这样的控制器中的所有标题:

    public function createAction($data) 
    {
        $request  = $this->request;
        print_r ($request->getHeaders());
        exit();

    }

我通过这样的卷曲发送请求:

curl -i -H "Accept: test" -H "Authorization: 123456" -H "Content-Type: qwerty" -X POST http://localhost/test

所有打印头,除此之外授权头。 我可以添加任意的头并将其打印出来 - 只是没有了“授权”头...

我也试图让()/有()的授权头,但它不存在。

Answer 1:

对我来说(ZF2 2.1.4版)正常工作:

curl -i -H "Accept: test" -H "Authorization: 123456" -X POST http://zf2.localhost

结果是:

HTTP/1.1 200 OK
Date: Thu, 11 Apr 2013 09:44:45 GMT
Server: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
X-Powered-By: PHP/5.4.7
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-ch
Pragma: no-cache
Content-Length: 843
Content-Type: text/html

object(Zend\Http\Headers)#168 (3) {
  ["pluginClassLoader":protected]=>
  NULL
  ["headersKeys":protected]=>
  array(4) {
    [0]=>
    string(9) "useragent"
    [1]=>
    string(4) "host"
    [2]=>
    string(6) "accept"
    [3]=>
    string(13) "authorization"
  }
  ["headers":protected]=>
  array(4) {
    [0]=>
    array(2) {
      ["name"]=>
      string(10) "User-Agent"
      ["line"]=>
      string(23) "User-Agent: curl/7.26.0"
    }
    [1]=>
    array(2) {
      ["name"]=>
      string(4) "Host"
      ["line"]=>
      string(24) "Host: zf2.localhost"
    }
    [2]=>
    array(2) {
      ["name"]=>
      string(6) "Accept"
      ["line"]=>
      string(12) "Accept: test"
    }
    [3]=>
    array(2) {
      ["name"]=>
      string(13) "Authorization"
      ["line"]=>
      string(21) "Authorization: 123456"
    }
  }
}

用下面的代码:

$request  = $this->getRequest();
var_dump($request->getHeaders());

使用以下方法来获得的价值:

$authVal = $request->getHeaders('authorization')->getFieldValue();

希望这可以帮助 :)



Answer 2:

我终于找到了答案在这里:

http://zend-framework-community.634137.n4.nabble.com/HTTP-Digest-authentication-does-not-work-with-PHP-as-CGi-td4658790.html

必须添加以下项目的.htaccess:

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]


Answer 3:

希望这将帮助你在ZF2

  $headers = $request->getHeaders();
  $authorization = $headers->get('Authorization')->getFieldValue();

$请求是对象Zend\Http\Request ;



文章来源: Getting request Authorization header in ZF2 controller