Getting token_not_provided when using Authorizatio

2019-02-19 07:16发布

I read this tutorial and 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 Authorization in the Header field and Bearer{white space}{token} in the value. The library I'm using is 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 dump that function, it shows that the Authorization Header is available. I tried with and without the .htaccess modification.

.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 ^(.*)/$ /$1 [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.

1条回答
Lonely孤独者°
2楼-- · 2019-02-19 07:45

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

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

it show be

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

Special Thanks to James-Daddies.

查看更多
登录 后发表回答