douqianke7467 2017-08-11 09:25
浏览 75
已采纳

在流明中创建多个Auth提供程序

I am trying to create multiple Auth Guards and services for my API. I want specific group to be available to specific users (more like RBAC without sessions).

If a user tries to access a group that has admin:auth as middleware its privileges will be checked. If it has api:auth then no privilege check.

I can't understand how to do this. I have added the following lines in the bootstrap/app.php

$app->routeMiddleware([
    'admin' => App\Http\Middleware\Admin::class,
    'api' => App\Http\Middleware\Api::class,
]);

And:

$app->register(App\Providers\AdminServiceProvider::class);
$app->register(App\Providers\ApiServiceProvider::class);

and created the Files Admin.php and APi.php in Middleware folder with the following content (basically same as Authenticate.php with name changes)

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;

class Admin
{
    /**
     * The authentication guard factory instance.
     *
     * @var \Illuminate\Contracts\Auth\Factory
     */
    protected $auth;

    /**
     * Create a new middleware instance.
     *
     * @param  \Illuminate\Contracts\Auth\Factory  $auth
     * @return void
     */
    public function __construct(Auth $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if ($this->auth->guard($guard)->guest()) {
            return response('Unauthorized.', 401);
        }

        return $next($request);
    }
}

And the AdminServiceProvider and ApiServiceProvider in the App\Providers folder with just this in function boot():

var_dump($this->app['api']);exit;

And this on top:

namespace App\Providers;

use App\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class ApiServiceProvider extends ServiceProvider

But I get the Following Error:

Error

What am I missing? I have already done composer dump-autoload, no difference.

Regards

  • 写回答

1条回答 默认 最新

  • douchun3680 2019-07-24 06:08
    关注

    Well what I ended up using is more like a work around, but I think this might be how it is supposed to be used.

    So what I did was that I created two groups (a parent and a child) with the middlewares I needed as follows:

    $app->group(["middleware" => "middleware1"], function() use ($app){
      $app->group(["middleware" => "middleware1"], function() use ($app){
        $app->post("/signin", "AppController@signin");
      }
    }
    

    This way, the signin route is reached after going through 2 middlewares.

    Regards

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程