请求头包丢失Authorization头中的Symfony 2?(Request headers b

2019-06-17 17:58发布

我想实现一个自定义的身份验证提供者在Symfony的2我送使用招和打印头的所有服务器端的测试请求; 还有, Authorization头部丢失。

难道我做错了什么?

GET /RESTfulBackend/web/index.php HTTP/1.1
Authorization: FID 44CF9590006BF252F707:jZNOcbfWmD/
Host: localhost
User-Agent: Fiddler
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3

监听器只是打印头和退出:

class HMACListener implements ListenerInterface
{
    private $securityContext;

    private $authenticationManager;

    public function handle(GetResponseEvent $event)
    {
        $request = $event->getRequest();
        print_r($request->headers->all()); 
        die();
     }
}

响应缺少Authorization头:

Array
(
    [host] => Array
        (
            [0] => localhost
        )
    [user-agent] => Array
        (
            [0] => Fiddler
        )
    [accept] => Array
        (
            [0] => text/html,application/xhtml+xml,application/xml
        )
    [accept-language] => Array
        (
            [0] => it-it,it;q=0.8,en-us;q=0.5,en;q=0.3
        )
)

Answer 1:

您必须将此代码添加到您的虚拟主机

    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

在虚拟主机标签工作,而不是在目录标签。



Answer 2:

Akambi的回答并没有为我工作,但发现在php网站这样的回答:

“解决方法下CGI / FastCGI的Apache的缺失Authorization头:

SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0

现在,如果客户端发送的授权头PHP会自动申报$ _ SERVER [* PHP_AUTH_]变量。”

由于derkontrollfreak + 9hy5l!



Answer 3:

经过验证的解决方案为我工作的时候,通过获取授权头。 然而,它产生一个空的授权报头时,有没有在进入的请求。 这是我如何解决它:

RewriteEngine On
RewriteCond %{HTTP:Authorization} .+
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]


Answer 4:

编写自定义一个公共API,当我有同样的问题, Authorization头。 要解决这个HeaderBag我使用的监听器:

namespace My\Project\Frontend\EventListener;

use Symfony\Component\HttpFoundation\HeaderBag;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;

/**
 * Listener for the REQUEST event. Patches the HeaderBag because the
 * "Authorization" header is not included in $_SERVER
 */
class AuthenticationHeaderListener
{
    /**
     * Handles REQUEST event
     *
     * @param GetResponseEvent $event the event
     */
    public function onKernelRequest(GetResponseEvent $event)
    {
        $this->fixAuthHeader($event->getRequest()->headers);
    }
    /**
     * PHP does not include HTTP_AUTHORIZATION in the $_SERVER array, so this header is missing.
     * We retrieve it from apache_request_headers()
     *
     * @param HeaderBag $headers
     */
    protected function fixAuthHeader(HeaderBag $headers)
    {
        if (!$headers->has('Authorization') && function_exists('apache_request_headers')) {
            $all = apache_request_headers();
            if (isset($all['Authorization'])) {
                $headers->set('Authorization', $all['Authorization']);
            }
        }
    }
}

并且绑定了kernel.request在服务定义:

services:
  fix_authentication_header_listener:
    class: My\Project\Frontend\EventListener\AuthenticationHeaderListener
    tags:
      - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest, priority: 255 }


Answer 5:

授权报头用于HTTP基本认证其通过阿帕奇格式无效丢弃如果不是。 尝试使用其他名称。



Answer 6:

另一种选择是努力为Apache 2.4时,其他选项没有被设置CGIPassAuth中的相关选项<Directory>背景下,这样的:

CGIPassAuth On

根据该文件,这是因为Apache 2.4.13可用。



Answer 7:

另一种解决方案是改变你的PHP处理程序运行PHP作为Apache Module而不是作为CGI application



Answer 8:

我们应授权在服务器端这个头“授权”,

它也简单地用nelmioCorsBundle完成nelmio_cors: defaults: allow_credentials: false allow_origin: [] allow_headers: [] allow_methods: [] expose_headers: [] max_age: 0 hosts: [] origin_regex: false forced_allow_origin_value: ~ paths: '^/api/': allow_origin: ['*'] allow_headers: ['Authorization']



文章来源: Request headers bag is missing Authorization header in Symfony 2?