我读到this tutorial我设法使它完美地工作。唯一的问题是,当我从 ?token={token_here} 切换到 Authorization Bearer {token here} 时,它会停止工作。 我正在使用 Postman,并在 header 字段中写入 Authorization ,并在值中写入 Bearer{white space}{token} 。 我正在使用的库是 https://github.com/tymondesigns/jwt-auth
我在 Wiki 中读到了有关 Apache 问题的信息,但我不确定这是否是我的情况。我用PHP函数诊断了它getallheaders() 。如果我死亡并转储该函数,则表明授权 header 可用。我尝试过使用和不使用 .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 ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
路线文件
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']);
});
});
});
对我哪里出错有什么想法吗?
提醒:如果我使用 http://localhost/projects/linus/public/api/test?token= {token}它有效。
最佳答案
事实证明,问题在于 .htaccess 文件中的 header 配置在两行中都缺少 *
。而不是
RewriteCond %{HTTP:Authorization} ^(.)
RewriteRule . - [e=HTTP_AUTHORIZATION:%1]
显示为
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
特别感谢James-Daddies .
关于php - 在 JWT Laravel 中使用授权 header 时获取 token_not_provided,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33101810/