47

我正在尝试在 Symfony 2 中实现自定义身份验证提供程序。我正在使用 Fiddler 发送测试请求并打印所有标头服务器端;好吧,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
        )
)
4

8 回答 8

61

您必须将此代码添加到virtualhost标记

如果您将它放在目录标签中,它将不起作用。

    RewriteEngine On
    RewriteCond %{HTTP:Authorization} ^(.*)
    RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
于 2013-06-18T17:31:04.843 回答
32

Akambi 的答案对我不起作用,但在 php 网站上找到了这个答案:

“在 CGI/FastCGI Apache 下缺少授权标头的解决方法:

SetEnvIf Authorization .+ HTTP_AUTHORIZATION=$0

现在,如果客户端发送授权标头,PHP 应该自动声明 $_SERVER[PHP_AUTH_*] 变量。”

谢谢derkontrollfreak+9hy5l!

于 2014-11-18T04:04:58.507 回答
20

经过验证的解决方案当时对我有用,可以通过 Authorization 标头。但是,当传入请求中没有 Authorization 标头时,它会生成一个空的 Authorization 标头。这就是我解决它的方法:

RewriteEngine On
RewriteCond %{HTTP:Authorization} .+
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
于 2015-01-05T19:20:35.393 回答
10

在编写带有自定义Authorization标头的公共 API 时,我遇到了同样的问题。为了修复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 }
于 2013-02-01T22:41:17.013 回答
8

授权标头用于http 基本身份验证,如果格式不正确,则会被 apache 丢弃。尝试使用其他名称。

于 2012-08-16T17:42:27.970 回答
6

当其他选项不起作用时,另一个适用于 Apache 2.4 的CGIPassAuth选项是在相关<Directory>上下文中设置选项,如下所示:

CGIPassAuth On

根据文档,它从 Apache 2.4.13 开始可用。

于 2016-07-04T00:14:05.597 回答
0

另一种解决方案是更改您的PHP处理程序以运行 PHP asApache Module而不是 as CGI application

于 2015-04-20T12:33:20.757 回答
0

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

它也可以简单地用 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']

于 2018-02-03T11:15:29.237 回答