在 JWT Laravel 中使用授权标头时获取 token_not_provided

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33101810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:29:41  来源:igfitidea点击:

Getting token_not_provided when using Authorization Header with JWT Laravel

phplaravellaravel-5.1jwt

提问by Marco Aurélio Deleu

I read this tutorialand I managed to make it work perfectly. The only problem is that when I switch from ?token={token_here}to Authorization Bearer {token here}it stops working. I'm using Postman and I'm writing Authorizationin the Header field and Bearer{white space}{token}in the value. The library I'm using is https://github.com/tymondesigns/jwt-auth

我阅读了本教程,并设法使其完美运行。唯一的问题是,当我从?token={token_here}切换到Authorization Bearer {token here} 时,它停止工作。我正在使用 Postman,我在 Header 字段中写了Authorization,在值中写了Bearer{white space}{token}。我正在使用的库是https://github.com/tymondesigns/jwt-auth

I read in the Wiki about the Apache problem and I'm not sure if that's my case. I diagnosed it with PHP function getallheaders(). If I die and dumpthat function, it shows that the Authorization Header is available. I tried with and without the .htaccess modification.

我在 Wiki 中阅读了有关 Apache 问题的信息,但不确定是否是我的情况。我用 PHP 函数getallheaders()诊断它。如果我死掉并转储该功能,则表明授权标头可用。我尝试使用和不使用 .htaccess 修改。

.htaccess

.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 If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ / [L,R=301]

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

Route File

路由文件

dd(getallheaders()['Authorization']);
Route::group(['prefix' => 'api'], function() {

    Route::post('login', 'AuthenticateController@email');

    Route::group(['middleware' => ['jwt.auth', 'jwt.refresh']], function() {
        Route::post('logout', 'AuthenticateController@logout');

        Route::get('test', function() {
            return response()->json(['foo' => 'bar']);
        });
    });
});

Any thoughts on where I'm going wrong?

关于我哪里出错的任何想法?

Reminder: if I use http://localhost/projects/linus/public/api/test?token={token}it works.

提醒:如果我使用http://localhost/projects/linus/public/api/test?token={token}它可以工作。

采纳答案by Marco Aurélio Deleu

As it turns out, the problem was that the header configuration in the .htaccess file was missing a *in both lines. Instead of

事实证明,问题在于 .htaccess 文件中的头配置*在两行中都缺少 a 。代替

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

it show be

它显示是

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

Special Thanks to James-Daddies.

特别感谢James-Daddies