Laravel Passport Route redirects to login page(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 重定向到登录页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel Passport Route 重定向到登录页面
基础教程推荐
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01