首页 文章

laravel 5.3 Api路由未返回“未授权访问”错误 .

提问于
浏览
1

我刚刚创建了一个新的 Laravel 5.3 项目并将以下路由添加到 routes/api.php 文件中 .

Route::get('/',function(){
    return view('welcome');
});

当我在浏览器上点击此URL http://localhost/api/ 时,我被导航到我的应用程序的默认laravel主页 .

现在我的问题是,当我尝试访问 api.php 文件中的路由而不传递令牌时,我是否应该收到"Unauthorized access"错误?即使我没有传递令牌,为什么laravel让我导航到api路线?

Note: I have not added laravel passport or any other oAuth libraries to the project yet.

1 回答

  • 1

    不,Laravel在定义这样的路线时不会默认检查这些 . 这是一件好事,因为在某些情况下您可能无需用户发送crsf令牌或进行身份验证即可提供信息 .

    你想要的是在你的路线中使用中间件 . 看看这里:https://laravel.com/docs/5.3/middleware

    这将在某些路由或路由组上使用指定的中间件,如“auth” . 即

    Route::group(['middleware' => ['auth']], function () {
    
        Route::post('profile', 'ProfileController@create');
        ....
    }
    

    另请参阅中间件组'web'页面上的示例

相关问题