简体   繁体   English

启用 Apache http 授权 header

[英]enable Apache http Authorization header

I write an API with PHP ZF2 they use HTTP Authorization.我写了一个APIPHP ZF2他们使用HTTP授权。 I fetch all HTTP Headers with apache_request_headers() (also tested with ZF2's $this->getRequest()->getHeaders()) .我使用apache_request_headers()获取所有HTTP头(也使用 ZF2 的$this->getRequest()->getHeaders())

It works on my locale installed version.它适用于我的语言环境安装版本。 But on my server the HTTP Authorization Header are not available.但是在我的服务器上 HTTP 授权 Header 不可用。 My Browser Debug tool show me that the Authorization header properly send.我的浏览器调试工具显示授权 header 已正确发送。

Both server are running with the same software: Ubuntu 14.04 with Apache2 (Server version: Apache/2.4.7 (Ubuntu)).两台服务器都运行相同的软件: Ubuntu 14.04Apache2 (服务器版本: Apache/2.4.7 (Ubuntu))。

Enabled apache2 modules ( auth_basic is enabled):启用apache2模块(启用auth_basic ): 启用 Apache2 模块

Is there a PHP ini setting to allow Authorization header?是否有PHP ini设置允许授权 header?

edit 2015-05-13:编辑 2015-05-13:

$headers = apache_request_headers();
if (isset($headers['Authorization'])) {
    echo 'you are auth';
} else {
    echo 'there is no Authorization';
}

On my locale system this returns 'you are auth', on the server 'there is no Authorization'.在我的语言环境系统上,这返回“你是授权”,在服务器上返回“没有授权”。 Tested with Postman app in Chrome browser.在 Chrome 浏览器中使用 Postman 应用进行测试。

edit 2015-05-14: I think it is an Apache2 topic.编辑 2015-05-14:我认为这是一个 Apache2 主题。

How can i enable the Authorization header in Apache2?如何在 Apache2 中启用授权 header?

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

in the .htaccess solves the problem.在 .htaccess 中解决了这个问题。

But i do not know why this is not necessary on my locale system.但我不知道为什么这在我的语言环境系统中不是必需的。

As bitkorn suggested, you can add the following to your .htaccess:正如 bitkorn 所建议的,您可以将以下内容添加到您的 .htaccess 中:

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

If that doesn't solve your problem, then you can try the following:如果这不能解决您的问题,那么您可以尝试以下操作:

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

However, something that must be mentioned is that if you're using either solution, you must access your header with the HTTP_AUTHORIZATION header.但是,必须提及的是,如果您使用任一解决方案,则必须使用HTTP_AUTHORIZATION标头访问标头。 If you try to use Authorization it will be null.如果您尝试使用授权,它将为空。

Server: Almalinux 8, Panel: WHM/CPANEL;服务器:Almalinux 8,面板:WHM/CPANEL;

The reason is apache. Something removes the header. To prevent;原因是 apache。有些东西删除了 header。为了防止;

Open httpd.conf打开 httpd.conf

<VirtualHost>
    # ...
    Include "/etc/apache2/conf.d/userdata/*.conf"
    # ...
</VirtualHost>
  • place will be detected by apache. And create a special conf to prevent removed automatically. place 会被apache检测到。并创建一个特殊的conf以防止自动删除。
nano /etc/apache2/conf.d/userdata/{username}.conf

Add this line;添加这一行;

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

Restart Apache;重启Apache;

service httpd restart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM