首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用基本授权作为中间件PSR-7PSR-15

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

Stack Overflow用户
提问于 2018-10-23 12:14:54
回答 1查看 982关注 0票数 4

TD;LR:我正在尝试理解中间件实现背后的想法。它似乎正在工作,但我如何正确地处理响应,以便它显示一个基本的授权登录提示到浏览器?

--

我在用:

运行以下代码:

代码语言:javascript
复制
$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);

返回以下内容:

代码语言:javascript
复制
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将显示如下实际的登录提示:

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-30 05:55:12

请将王国改为“我的王国”。

代码语言:javascript
复制
$middleware = [
    new \Middlewares\ResponseTime(),
    (new \Middlewares\BasicAuthentication([
        'username1' => 'password1',
        'username2' => 'password2'
    ]))->attribute('username')->realm('My realm')
];

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

你仍然可以用口香糖测试它,如下所示。

下面的代码将起作用。

代码语言:javascript
复制
$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

下面的代码将失败。

代码语言:javascript
复制
$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的最佳方法是在中间件框架内。

Zend表达

代码语言:javascript
复制
   //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"
        ]
    ]));

上述两种代码都按预期工作。例如,提示浏览器中的用户名和密码,并允许用户在发送正确的凭据时查看内容。口香糖会给你带来麻烦。

我希望这能帮到你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52948852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档