Laravel Passport Route 重定向到登录页面

Laravel Passport Route redirects to login page(Laravel Passport Route 重定向到登录页面)
本文介绍了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');

推荐答案

重定向到定义的登录路由发生在 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'));
}

该函数试图检测它是否被 API 调用(在这种情况下它返回一个带有 JSON 消息的 401 Unauthorized 响应),如果不是,它将根据它的评论重定向到登录页面

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 重定向到登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)