Laravel Passport Route重定向到登录页面 [英] Laravel Passport Route redirects to login page

查看:420
本文介绍了Laravel Passport Route重定向到登录页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5.3&护照.

I'm using Laravel 5.3 & Passport.

使用Postman测试我在api.php文件中设置的任何路由时,它总是返回登录页面.这是我的测试路线的示例:

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');

邮递员参数:

Accept application/json
Authorization Bearer <then my key>

我在搜索答案时找到的另一个解决方案中,将中间件设置为"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'));
    }

我已经尝试了几乎所有对其他人有用的解决方案,但仍然没有运气.任何建议将不胜感激.

I've tried just about every solution that has worked for others but still no luck. Any suggestions would be much appreciated.

更新 所以我终于有了一些工作.我创建了一个消费者应用程序,并创建了一些测试功能.我能够使用带有令牌验证的api.但是,点击此路由不再返回我的登录页面,而是现在不返回任何内容.因此,无论出于何种原因,它仍然无法正常工作.

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');

推荐答案

在app \ Exceptions \ Handler.php类中发生了对已定义登录路由的重定向.

The redirection to the defined login route is occurring in the app\Exceptions\Handler.php class.

protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    return redirect()->guest(route('login'));
}

该函数尝试检测是否正在从API调用它(在这种情况下,它会返回401带有JSON消息的未授权响应),如果没有,它将根据注释将其重定向到登录页面

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

将身份验证异常转换为未经身份验证的响应

Converts an authentication exception into an unauthenticated response

要解决邮递员中的问题,请在请求中单击标题"选项卡并添加:

To resolve the issue in postman, on the request click on the Headers tab and add:

key:   Accept
value: application/json

我对此很陌生,因此不确定是用Postman测试所有API调用时应该添加的标头,还是仅是该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.

无论如何,通过重定向到登录页面可以解决您的问题,但这是您的基础身份验证不起作用的标志

Anyway this would solve your issue with being redirected to the login page, however it's a sign your underlying authentication isn't working

这篇关于Laravel Passport Route重定向到登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆