【问题标题】:Laravel cannot protect API routesLaravel 无法保护 API 路由
【发布时间】:2018-06-28 14:47:10
【问题描述】:

我的routes/api.php 中有以下路线:

Route::group(['middleware' => ['auth']], function () {
    Route::get('users/', 'Api\UserController@index');
});

这会不断将我重定向到我的仪表板。

然后我试试这个:

 Route::get('users/', 'Api\UserController@index')->middleware('auth');

这可行,但它不保护路由,所以如果我退出,我仍然可以访问它。

任何想法为什么会这样?我不确定验证 API 路由的最佳方式是什么,通常的约定是什么?

我正在使用 Laravel 5.5

【问题讨论】:

    标签: laravel api laravel-5 routing


    【解决方案1】:

    您不能在api.php 路由中使用auth 中间件,只能在web.php 中使用。但是你可以使用auth:api 中间件。

    https://laravel.com/docs/5.5/passport#protecting-routes

    【讨论】:

    • 我明白了,在文档中的任何地方都看不到。那么这是否意味着我必须使用 Passport 呢?
    • 我曾尝试像这样使用auth:apiRoute::get('/users', 'Api\UserController@index')->middleware('auth:api');但它会将我重定向到我的仪表板。我需要在 AuthServiceProvider 或任何其他配置中进行更改吗?
    • 你不需要专门使用护照,还有许多其他的 api auth 包。这个比较常用:github.com/tymondesigns/jwt-auth
    猜你喜欢
    • 2019-09-13
    • 1970-01-01
    • 2017-10-12
    • 2021-11-12
    • 2023-03-28
    • 2015-03-11
    • 2018-09-12
    • 2017-02-11
    • 2016-04-15
    相关资源
    最近更新 更多