doujiang1939 2016-08-26 19:00
浏览 116

Laravel 5.3 Passport路由使用Web中间件

Laravel 5.3 Passport component looks pretty cool, but I have some confusion on it.

On documentation Passport is under API Authentication, and when set config/auth.php, it will change driver to be 'passport' of guards 'api'.

Laravel 5.3 have web.php and api.php to differ routes group to use web middleware or api middleware.

After installed Passport, there's a step to add Passport:routes() to AuthServiceProvider. When I run route:list it will show the new routes are all using web and auth middleware.

My question is why these passport routes using web middleware? For my understand Passport should work for stateless APIs authentication, but web middleware is not.

  • 写回答

2条回答 默认 最新

  • doujia6433 2016-09-06 09:35
    关注

    I seem to have solved this.

    First thing I did was add and configure a CORS middleware from here https://github.com/barryvdh/laravel-cors

    Then I wrapped the Passport routes in a route group by editing app/Providers/AuthServiceProvider.php

    /** 
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {   
        $this->registerPolicies();
    
        Route::group(['prefix' => 'api', 'middleware' => 'cors'], function() {
    
            Passport::routes();
    
        }); 
    }
    

    In theory, if you wanted to use the oauth service outside of your API, you could have two instances of the Passport routes, one with the /api prefix and the cors middleware, then one without, so you don't lose cross-origin protection when using oauth in the browser.

    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题