[英]Passport authentication is not working in laravel 5.3
I have set-up Laravel using passport as per the documentation here: 我已经按照这里的文档使用护照设置了Laravel:
https://laravel.com/docs/5.3/passport . https://laravel.com/docs/5.3/passport 。
I have written one route in API route and send request http://localhost/laravel_project/public/api/user
using postman but its showing me below error: 我已经在API路由中编写了一条路由,并使用邮递员发送请求
http://localhost/laravel_project/public/api/user
但它向我显示了以下错误:
NotFoundHttpException in RouteCollection.php line 161:
RouteCollection.php第161行中的NotFoundHttpException:
I have the following route (in routes/api.php): 我有以下路线(在routes / api.php中):
Route::get('/user', function (Request $request) {
return array(
1 => "John",
2 => "Mary",
3 => "Steven"
);
})->middleware('auth:api');
but when I removed ->middleware('auth:api')
line in the route it's working fine for me. 但是当我在路由中删除
->middleware('auth:api')
行时,对我来说工作正常。
How can I fix this? 我怎样才能解决这个问题?
Also please tell me if I don't want to add passport authentication in my some routes how can i do this? 还请告诉我,如果我不想在某些路线中添加护照认证,该怎么办?
I was having the same problem, it seems you have to specify the Accept header to application/json
as shown by Matt Stauffer here 我有同样的问题,看来你必须指定接受头
application/json
如图马特·斯托弗在这里
Some further notes: 其他注意事项:
/login
but probably you haven't done PHP artisan make:auth
so it wont find the login route. /login
但可能您还没有完成PHP artisan make:auth
因此找不到登录路径。 Route::group
and auth:api
as the middleware Route::group
和auth:api
作为中间件将它们Route::group
In your routes/api.php
you can do this: 在您的
routes/api.php
您可以执行以下操作:
Route::group(['middleware' => 'auth:api'], function(){
Route::get('/user', function (Request $request) {
return array(
1 => "John",
2 => "Mary",
3 => "Steven"
);
});
});
All the routes you define inside this group will have the auth:api
middleware, so it will need passport authentication in order to access to it. 您在该组内定义的所有路由都将具有
auth:api
中间件,因此将需要通行证认证才能访问它。
Outside of this group you can put your api routes that doesn't need authentication. 在该组之外,您可以放置不需要身份验证的api路由。
EDIT: In order to make sure that the route actually exists with the required middleware, run php artisan route:list
. 编辑:为了确保所需的中间件实际上存在该路由,请运行
php artisan route:list
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.