简体   繁体   English

护照认证在Laravel 5.3中不起作用

[英]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: 其他注意事项:

  1. Your default Accept header is set to text/html, therefore Laravel will try redirect you to the url /login but probably you haven't done PHP artisan make:auth so it wont find the login route. 您的默认Accept标头设置为text / html,因此Laravel会尝试将您重定向到url /login但可能您还没有完成PHP artisan make:auth因此找不到登录路径。
  2. When you remove the middleware it will work because you are no longer authenticating your request 删除中间件后,它将不再起作用,因为您不再对请求进行身份验证
  3. To authenticate some routes, just group them using Route::group and auth:api as the middleware 要对某些路由进行身份验证,只需使用Route::groupauth: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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM