falco442/slim-multirole-authentication-authorization

具有多角色功能的基本 http 认证/授权中间件

dev-master 2018-11-12 17:07 UTC

This package is not auto-updated.

Last update: 2024-09-27 19:42:41 UTC


README

简介

此中间件是为 Slim 骨架应用 制作的。它使用 Eloquent 来实现其功能,因此你应当遵循 Slim 3 的指南章节 使用 Eloquent 与 Slim 来使用与 Eloquent 相结合的 Slim,即 Laravel ORM。

此中间件验证了 Authorization 头部的存在,该头部由 W3 协议确定,并阻止/允许其他请求。

安装

使用 composer 获取

composer require falco442/slim-multirole-authentication-authorization

使用

然后你可以使用你的设置引导你的应用程序

require 'vendor/autoload.php';

use falco442\Middleware\Authentication\BasicHttpAuthentication;

// Create and configure Slim app
$config = [
    'settings' => [
        'addContentLengthHeader' => false,
        'displayErrorDetails' => true,
        // other settings
        'authentication' => [
            'userModel' => 'your-eloquent-model-with-namespace', // for example 'App\\Model\\User'
            'fields' => [
                'username' => 'email',
                'password' => 'password'
            ],
            'jsonResponse' => true, // if you want response in json format
            'unauthorizedMessage' => 'Your unauthorized message',
            'unauthorizedStatus' => 403 // Usually 403
        ]
    ]
];
$app = new \Slim\App($config);

$capsule = new \Illuminate\Database\Capsule\Manager;
$capsule->addConnection($config['settings']['db']);

$capsule->setAsGlobal();
$capsule->bootEloquent();


$app->get('/',function($request, $response, $args){
    return $response->withJson([1,2,3]);
})->add((new BasicHttpAuthentication($app->getContainer())));

// Run app
$app->run();