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 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划