crtl / slim-auth-middleware
用于自定义授权的简单中间件。
1.0.3
2018-10-23 11:43 UTC
Requires
- php: ^7.0
- slim/slim: ^3.0
This package is auto-updated.
Last update: 2024-09-24 04:10:33 UTC
README
此包提供了一个基本的中间件,用于实现任何类型的授权。
安装
composer require crtl/slim-auth-middleware
使用
该包已自带对HTTP-Basic Authentication的预构建实现。
<?php use Crtl\AuthorizationMiddleware\BasicAuthorization; $app = new \Slim\App(); $app->add( new BasicAuthorization([ BasicAuthorization::CONFIG_ENABLE => true, BasicAuthorization::CONFIG_USER => "secret", BasicAuthorization::CONFIG_SECRET => "password" ]) ); $app->get("/", function($request, $response) use ($app) { $body = $response->getBody(); $body->write("Authorized"); return $response->withBody($body), }); $app->run();
如果请求已授权,则应用程序将调用下一个中间件。否则,将调用 Crtl\AuthorizationMiddleware\AbstractAuthorization::getErrorResponse
。
要实现自定义授权,只需扩展 Crtl\AuthorizationMiddleware\AbstractAuthorization
并实现
protected isAuthorized() : bool
方法。
<?php class CustomAuthorization extends \Crtl\AuthorizationMiddleware\AbstractAuthorization{ protected function isAuthorized(): bool { /* @var \Psr\Http\Message\ResponseInterface $response */ $response = $this->response; /* @var \Psr\Http\Message\RequestInterface $request */ $request = $this->request; /* check if authorized */ return true; } }