简体   繁体   English

在流明中创建多个Auth Provider

[英]Creating Multiple Auth Providers in Lumen

I am trying to create multiple Auth Guards and services for my API. 我正在尝试为我的API创建多个Auth Guard和服务。 I want specific group to be available to specific users (more like RBAC without sessions). 我希望特定的组对特定的用户可用(更像是没有会话的RBAC)。

If a user tries to access a group that has admin:auth as middleware its privileges will be checked. 如果用户尝试访问具有admin:auth作为中间件的组,则将检查其特权。 If it has api:auth then no privilege check. 如果它具有api:auth则没有特权检查。

I can't understand how to do this. 我不明白该怎么做。 I have added the following lines in the bootstrap/app.php 我在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) 并在Middleware文件夹中创建了具有以下内容的文件Admin.phpAPi.php (与更改名称的Authenticate.php基本相同)

<?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() : App \\ Providers文件夹中的AdminServiceProviderApiServiceProvider在功能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: 但是我收到以下错误:

错误

What am I missing? 我想念什么? I have already done composer dump-autoload , no difference. 我已经做过composer dump-autoload ,没有区别。

Regards 问候

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. 这样,在经过2个中间件之后便达到了signin路径。

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM