doupo6967 2019-06-17 10:45
浏览 237
已采纳

除了某人之外的所有路由的Slim Authentication中间件

I would like to have a Slim middleware to check authentication on all requests but some specific ones (for example login page). I prepared the first AuthenticationMiddleware middleware to check all pages:

$app->add(new \App\Middleware\AuthenticationMiddleware($container));

Then I create another one AnonymousMiddleware that it is supposed to add a variable to set the exception to authentication checks:

$app->group('',function() use ($app){
    $app->get('/','LogicController:index');    
})->add(new AnonymousMiddleware($container));

The problem is that routes middleware (AnonymousMiddleware) is applied AFTER the general middleware (AuthenticationMiddleware);

I tried to use determineRouteBeforeAppMiddleware but it doesn't change the result.

I know I would set a route middleware for all authenticated routes but sounds a bit dangerous if I forget it, so, I would prefer to set which router are not under authentication then keep the check all other routes.

  • 写回答

1条回答

  • duanlu7680 2019-07-22 04:33
    关注

    you need to chose different approach

    you need to wrap all routes which should be "protected via Auth middleware" and exclude = not wrap routes which should not be handled by this middleware

    you can add a group where all your routes will live and only login (and logout) route will be outside of this group ;)

    something like

    $app->group("/api/v1", function() { 
     // all your protected routes definitions here
    })
    ->add(AuthenticationMiddleware::class)); // wrap by middleware
    
    $app->post('/login', function(){});
    
    $app->add(new MiddlewareForAllRoutes()); // middlewares for all routes
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料