[英]Laravel Passport API Authentication not working with Bearer Token
我目前正在開發Laravel應用程序。 在應用程序內部,我需要創建一個公共api,如果客戶端獲得了訪問令牌,則需要可以對其進行訪問。
因此,我正在使用laravel / passport軟件包。
檢索access_token工作正常。 關鍵部分是使用此訪問令牌進行身份驗證,並從受保護的路由獲取json數據作為響應。
安裝程序如下:
使用Laravel 5.6.30和Passport 6.0.7時,cookie序列化被禁用。
/app/Providers/AuthServiceProvider.php
$this->registerPolicies();
Passport::withoutCookieSerialization(); // because of the Laravel Update
Passport::routes();
Passport::tokensExpireIn(Carbon::now()->addDays(20));
Passport::refreshTokensExpireIn(Carbon::now()->addDays(20));
如前所述在這里我改變了令牌的到期時間,使他們不溢出或用開始-
在這里 @adamj提到要看看mapApiRoutes()
/app/Providers/RouteServiceProviders.php
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('auth:api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
我向我的API發送的請求設置了以下標頭:
Accept: application/json
Authorization: Bearer {my_access_token}
我嘗試在郵遞員內訪問的路線如下:
Route::group(['middleware' => ['auth:api']], function () {
Route::get('/hello/world', function () {
return 'Hello World';
});
});
這樣,響應始終是401的HTTP狀態。有人可以幫忙嗎?
我真的不知道如何解決這個問題。
謝謝問候!
該錯誤不在Laravel內部。 該錯誤與郵遞員有關。
在“授權”選項卡中,您可以選擇一種類型。 除非您不選擇No Auth,否則您手動設置的Header將被Postman取代。
希望這可以對任何人有幫助。
這是因為默認情況下,Apache不會將授權標頭傳遞給PHP。 我找到了這個解決方案:
https://support.deskpro.com/cn/kb/articles/missing-authorization-headers-with-apache
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.