【问题标题】:Laravel Passport API Status Code:403 ForbiddenLaravel Passport API 状态码:403 Forbidden
【发布时间】:2018-04-11 01:16:35
【问题描述】:

我正在开发一个 Ionic 移动应用程序,它通过 REST API 连接到 Laravel 中的服务器。我使用了 Laravel 护照包并配置了一些关于授权令牌如何在 API 上工作的内容。因此,我为 Ionic 应用程序的提供程序编写了一些代码,并且我为移动设备中的用户提供了这个登录功能,以便通过服务器的 API 登录,但它一直告诉我这个错误:

状态码:403 禁止

这似乎是服务器端错误,我无权访问此资源。对吧?

所以服务器理解了来自 Ionic 端的请求,但拒绝或被服务器禁止。

我使用 CORS Allow-Control-Origin 将 Ionic 应用程序连接到 Laravel。

但是,当我尝试使用 POSTMAN 使用 Laravel 护照包提供的所有授权类型和客户端机密进行 POST 请求时,它成功地为我提供了令牌类型持有者、访问令牌和 oauth 的刷新令牌。

这是我的中间件 Cors 中的代码

<?php

namespace App\Http\Middleware;

use Closure;

class Cors
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        return $next($request)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    }
}

这是我的 api 路由中的代码:

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:api', 'cors')->get('/user', function (Request $request) {
    return $request->user();
});


Route::resource('departments', 'DepartmentAPIController');
Route::resource('users', 'UserAPIController');
Route::get('users/{username}', 'UserAPIController@getAccount');

Route::resource('inspection_checklists', 'InspectionChecklistsAPIController');

顺便说一句,我在用户 API 控制器中使用 getAccount 函数。

感谢有人可以提供帮助。 提前致谢。

【问题讨论】:

    标签: php laravel api cors laravel-5.4


    【解决方案1】:

    与 CORS 无关。 如护照文件所述:

    JSON API 由webauth 中间件保护;因此,它只能从您自己的应用程序中调用。无法从外部源调用它。 这意味着您必须登录才能使用这些路线。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-24
      • 2018-09-08
      • 1970-01-01
      • 2019-03-05
      • 2019-05-18
      • 2020-05-29
      • 2018-11-05
      • 2022-10-17
      相关资源
      最近更新 更多