使用基本授权作为中间件 PSR-7 PSR-15

IMB*_*IMB 5 php architecture middleware

TD;LR:我试图理解中间件实现背后的想法。它似乎有效,但我如何正确处理响应,以便它向浏览器显示基本授权登录提示?

--

我在用着:

运行下面的代码:

$middleware = [
    new \Middlewares\ResponseTime(),
    (new \Middlewares\BasicAuthentication([
        'username1' => 'password1',
        'username2' => 'password2'
    ]))->attribute('username')
];

// Default handler for end of collection
$default = function (\GuzzleHttp\Psr7\ServerRequest $request) {
    // Any implementation of PSR-7 ResponseInterface
    return new \GuzzleHttp\Psr7\Response();
};

$collection = new \Equip\Dispatch\MiddlewareCollection($middleware);

// Any implementation of PSR-7 ServerRequestInterface
$request = \GuzzleHttp\Psr7\ServerRequest::fromGlobals();
$response = $collection->dispatch($request, $default);

print_r($response);
Run Code Online (Sandbox Code Playgroud)

返回以下内容:

GuzzleHttp\Psr7\Response Object
(
    [reasonPhrase:GuzzleHttp\Psr7\Response:private] => Unauthorized
    [statusCode:GuzzleHttp\Psr7\Response:private] => 401
    [headers:GuzzleHttp\Psr7\Response:private] => Array
        (
            [WWW-Authenticate] => Array
                (
                    [0] => Basic realm="Login"
                )

            [X-Response-Time] => Array
                (
                    [0] => 16.176ms
                )

        )

    [headerNames:GuzzleHttp\Psr7\Response:private] => Array
        (
            [www-authenticate] => WWW-Authenticate
            [x-response-time] => X-Response-Time
        )

    [protocol:GuzzleHttp\Psr7\Response:private] => 1.1
    [stream:GuzzleHttp\Psr7\Response:private] => 
)
Run Code Online (Sandbox Code Playgroud)

看起来middlewares/response-timemiddlewares/http-authentication执行得很好。但是,我的印象是middlewares/http-authentication会显示这样的实际登录提示:

在此输入图像描述

但事实并非如此。我应该自己实现吗?如果是,我该如何正确地做到这一点?

Kam*_*oni 1

请将领域更改为“我的领域”。

$middleware = [
    new \Middlewares\ResponseTime(),
    (new \Middlewares\BasicAuthentication([
        'username1' => 'password1',
        'username2' => 'password2'
    ]))->attribute('username')->realm('My realm')
];
Run Code Online (Sandbox Code Playgroud)

GuzzleHttp 请求发生在后端,因此它不会像通常在浏览器中那样工作,例如提示输入用户名和密码。当您使用 Guzzle 而不使用任何浏览器时,您基本上是在模仿请求。因此,如果您想要提示,则必须在不使用 GuzzleHttp 的情况下实现此逻辑。

您仍然可以使用 Guzzle 进行测试,如下所示。

下面的代码将起作用。

$request = \GuzzleHttp\Psr7\ServerRequest::fromGlobals()->withHeader('Authorization', 'Basic '.base64_encode('username1:password1'));
$response = $collection->dispatch($request, $default);
echo $response->getStatusCode(); //200
echo $response->getReasonPhrase(); // OK
Run Code Online (Sandbox Code Playgroud)

下面的代码将会失败。

$request = \GuzzleHttp\Psr7\ServerRequest::fromGlobals()->withHeader('Authorization', 'Basic '.base64_encode(''IncorrectUser:IncorrectPassword''));
$response = $collection->dispatch($request, $default);
echo $response->getStatusCode(); //401
echo $response->getReasonPhrase(); // Unauthorized
Run Code Online (Sandbox Code Playgroud)

实现的最佳方法BasicAuthentication是在中间件框架内。

Zend 表达

   //Example using your library
   $app->pipe((new \Middlewares\BasicAuthentication([
                    'username1' => 'password1',
                    'username2' => 'password2'
                    ])
            )->attribute('username')->realm('My realm'));

    //Example using other library
    $app->pipe(new Tuupola\Middleware\HttpBasicAuthentication([
        "users" => [
            "root" => "t00r",
            "user" => "passw0rd"
        ]
    ]));
Run Code Online (Sandbox Code Playgroud)

上面的代码都按预期工作。例如,在浏览器中提示输入用户名和密码,并允许用户在发送正确的凭据时查看内容。Guzzle 给您带来了问题。

我希望这有帮助。