简体   繁体   English

Laravel 获取请求头

[英]Laravel get request headers

I am using POSTMAN to send a GET request into the api with a header containing Authorization.我正在使用 POSTMAN 将 GET 请求发送到带有包含授权的标头的 api 中。

I know the data header works because if it doesn't the route returns a 401 error.我知道数据标头有效,因为如果无效,路由将返回 401 错误。

I wanted to get the Authorization header like so:我想像这样获得授权标头:

$access_token = Request::header('Authorization');

But noticed that it returns NULL.但是注意到它返回NULL。

So I tried to catch the values with:所以我试图用以下方法捕捉值:

die(var_dump(Request::header()));

And noticed that it doesn't contain any Authorization header.并注意到它不包含任何 Authorization 标头。 Just host to cookie headers.只是托管到 cookie 标头。


update更新

Should get Authorization: Bearer ACCESS TOKEN应该得到Authorization: Bearer ACCESS TOKEN

What POSTMAN Version did you use?你用的是什么邮递员版本?

Are you on your local machine or managed server, some hosting companies don't allow AUTHORIZATION HEADER.您是在本地计算机还是托管服务器上,某些托管公司不允许 AUTHORIZATION HEADER。

.htaccess modification .htaccess 修改

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

The answer from dschniepp is right, but I have problems with this too. dschniepp 的答案是正确的,但我也有这个问题。 You have to do two things:你必须做两件事:

  1. Check if mod_rewrite is available and activated.检查 mod_rewrite 是否可用并激活。
  2. Update the .htaccess file of Laravel, located in the public folder.更新 Laravel 的 .htaccess 文件,位于 public 文件夹中。

In the first point you have to check if the "mod_rewrite" module is available through php_info function, in a separate php file.首先,您必须在单独的 php 文件中检查“mod_rewrite”模块是否可以通过 php_info 函数使用。 Then if it is available you have to activate it, that depends on the configuration of your webserver, in my Nitrous box I added these lines to my httpd.conf file:然后,如果它可用,您必须激活它,这取决于您的网络服务器的配置,在我的 Nitrous 框中,我将这些行添加到我的 httpd.conf 文件中:

<IfModule mod_rewrite>
   RewriteEngine On
</IfModule>

Or you can activate the module in the .htaccess file too:或者您也可以激活 .htaccess 文件中的模块:

RewriteEngine On

Then in the same .htaccess file located in public folder in the root of the laravel app, you have to add these lines:然后在位于 Laravel 应用程序根目录的公共文件夹中的同一个 .htaccess 文件中,您必须添加以下几行:

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

These lines worked for me.这些线路对我有用。 Your .htaccess file should look like this:您的 .htaccess 文件应如下所示:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

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

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

And that's it, you should have the Authorization header in the Request::header() array.就是这样,您应该在 Request::header() 数组中有 Authorization 标头。 Just to clarify these is an issue with Apache, not with Laravel itself.只是为了澄清这些是 Apache 的问题,而不是 Laravel 本身的问题。

In Laravel 5.5 you can read herders by using apache_request_headers simply read it in your controller by the following lines在 Laravel 5.5 中,您可以使用 apache_request_headers 读取牧羊人,只需通过以下几行在您的控制器中读取它

$headers = apache_request_headers();
dd($headers['Authorization']);

Make sure you have added use Illuminate\\Http\\Request;确保您已添加 use Illuminate\\Http\\Request; in your controller在你的控制器中

Missing authorization headers with Apache virtual host.缺少 Apache 虚拟主机的授权标头。

Apart of the solution above the culprit may be because Apache server does not allow authorization header to pass through virtual host.除了上面的解决方案,罪魁祸首可能是因为 Apache 服务器不允许授权标头通过虚拟主机。

To solve this issue you have to add the line allowing Apache to pass authorization header to PHP in you virtual hosts configuration.要解决此问题,您必须在虚拟主机配置中添加允许 Apache 将授权标头传递给 PHP 的行。 Eg for Ubuntu 18.04 the virtual host is defined in /etc/apache2/sites-available/your-site-name.conf , see this tutorial for better context .例如,对于 Ubuntu 18.04,虚拟主机在/etc/apache2/sites-available/your-site-name.conf定义, 请参阅本教程以获得更好的上下文

<VirtualHost>
    # ...
    SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
    # ...
</VirtualHost>

After updating the virtual host config do not forget to restart Apache (again eg Ubuntu 18.04 sudo systemctl restart apache2 ).更新虚拟主机配置后不要忘记重新启动 Apache(再次例如 Ubuntu 18.04 sudo systemctl restart apache2 )。

This should fix the issue.这应该可以解决问题。

Here is the original answer .这是原始答案

Posting this here as it solved my problem.在这里发布它,因为它解决了我的问题。 This applies for sub domains but can obviously be adjusted for plain domains as well.这适用于子域,但显然也可以针对普通域进行调整。 Applied this within my routes file at the top.在顶部的路由文件中应用了这个。

$newUrl = '';
try{
    $urlParts = parse_url($_SERVER['HTTP_REFERER']) ?? '';
    $newUrl = $urlParts['scheme'] . "://" . $urlParts['host'];
    if(!stristr($newUrl, '.yourdomain.com')){
        $newUrl = 'false';
    }
}catch(Exception $e)
{}
header('Access-Control-Allow-Origin: ' . $newUrl);
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Headers: access-control-allow-origin,cache-control,content-type,postman-token');

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

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