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

时间:2018-10-23 12:14:55

标签: 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);

返回以下内容:

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] => 
)

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

enter image description here

但是没有。我想自己实现吗?如果是,我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

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

$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

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

您仍然可以按照以下方法使用Guzzle对其进行测试。

  

下面的代码将起作用。

$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
  

以下代码将失败。

BasicAuthentication

实现 //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" ] ])); 的最佳方法是在中间件框架内。

  

Zend Expressive

$colors: ( 
    -black: #000,
    -blue: #088DC6
);

@mixin generateBgColor($colorName) {
    .bg-color#{$colorName} {
        background-color: map-get($colors, $colorName);
    }
}

@include generateBgColor("-blue");

以上两个代码均按预期工作。例如在浏览器中提示输入用户名和密码,并在发送正确的凭据后允许用户查看内容。食欲给您带来麻烦。

我希望这会有所帮助。