問題描述
我使用的是 Laravel 5.3 &護(hù)照.
I'm using Laravel 5.3 & Passport.
當(dāng)使用 Postman 測試我在 api.php 文件中設(shè)置的任何路由時,它總是返回登錄頁面.這是我的測試路線示例:
When using Postman to test any route I have set in api.php file it always returns the login page. Here is an example of my testing route:
Route::get('/getKey', function() {
return 'hello';
})->middleware('client_credentials');
郵遞員參數(shù):
Accept application/json
Authorization Bearer <then my key>
根據(jù)我在搜索答案時找到的另一個解決方案,我已將中間件設(shè)置為auth:api".
I have set middleware to 'auth:api' per another solution I found while searching for the answer.
protected function mapApiRoutes()
{
Route::prefix('api')
->middleware('auth:api')
->namespace($this->namespace)
->group(base_path('routes/api.php'));
}
我已經(jīng)嘗試了幾乎所有對其他人有效的解決方案,但仍然沒有運(yùn)氣.任何建議將不勝感激.
I've tried just about every solution that has worked for others but still no luck. Any suggestions would be much appreciated.
更新所以我終于得到了一些工作.我創(chuàng)建了一個消費(fèi)者應(yīng)用程序并創(chuàng)建了一些測試功能.我能夠使用 api,并驗(yàn)證令牌.然而,點(diǎn)擊這條路線不再返回我的登錄頁面,而是現(xiàn)在什么都不返回.因此,無論出于何種原因,它仍然無法正常工作.
UPDATE So I finally got something to work. I created a consumer app and created a few test functions. I was able to consume the api, with verification of token. However, hitting this Route no longer returns my login page, but instead now returns nothing. So its still not working for whatever reason.
Route::get('/user', function (Request $request) {
return $request->user();
})->middleware('client_credentials');
推薦答案
重定向到定義的登錄路由發(fā)生在 appExceptionsHandler.php 類中.
The redirection to the defined login route is occurring in the appExceptionsHandler.php class.
protected function unauthenticated($request, AuthenticationException $exception)
{
if ($request->expectsJson()) {
return response()->json(['error' => 'Unauthenticated.'], 401);
}
return redirect()->guest(route('login'));
}
該函數(shù)試圖檢測它是否被 API 調(diào)用(在這種情況下它返回一個帶有 JSON 消息的 401 Unauthorized 響應(yīng)),如果不是,它將根據(jù)它的評論重定向到登錄頁面
The function tries to detect whether it is being called from an API (it which case it returns a 401 Unauthorized reponse with JSON message) and if not it will redirect to the login page according to the comments it
將身份驗(yàn)證異常轉(zhuǎn)換為未經(jīng)身份驗(yàn)證的響應(yīng)
Converts an authentication exception into an unauthenticated response
要解決郵遞員中的問題,請在請求中單擊標(biāo)題"選項(xiàng)卡并添加:
To resolve the issue in postman, on the request click on the Headers tab and add:
key: Accept
value: application/json
我對此很陌生,所以不確定這是我們應(yīng)該在使用 Postman 測試所有 API 調(diào)用時添加的標(biāo)頭,還是只是對如何設(shè)置此 laravel 方法感到不快.
I'm pretty new to this so am not sure whether this is a header we should be adding when testing all API calls with Postman or just a nusience with how this laravel method is setup.
無論如何,這將解決您被重定向到登錄頁面的問題,但這表明您的基礎(chǔ)身份驗(yàn)證不起作用
Anyway this would solve your issue with being redirected to the login page, however it's a sign your underlying authentication isn't working
這篇關(guān)于Laravel Passport Route 重定向到登錄頁面的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!