php – 使用基本授权作为中间件PSR-7 PSR-15
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php – 使用基本授权作为中间件PSR-7 PSR-15,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3294字,纯文字阅读大概需要5分钟。
内容图文
![php – 使用基本授权作为中间件PSR-7 PSR-15](/upload/InfoBanner/zyjiaocheng/768/47e4c3f7cc22459aa8f53cb7a0aa89dd.jpg)
TD; LR:我正在努力掌握中间件实施背后的想法.它似乎正在工作,但我如何正确处理响应,以便它向浏览器显示基本授权登录提示?
–
我正在使用:
> equip/dispatch
对于PSR-15中间件调度程序
> guzzlehttp/psr7
对于PSR-7 ServerRequestInterface
> middlewares/http-authentication
示例基本授权中间件
> middlewares/response-time
额外的虚拟中间件(检查是否也在管道中执行)
运行以下代码:
$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] =>
)
看来中间件/响应时间和中间件/ http认证执行得很好.但是,我认为中间件/ http-authentication会显示如下的实际登录提示:
但事实并非如此.我想自己实现它?如果是,我该如何正确地做到这一点?
解决方法:
请将领域更改为“我的领域”.
$middleware = [
new \Middlewares\ResponseTime(),
(new \Middlewares\BasicAuthentication([
'username1' => 'password1',
'username2' => 'password2'
]))->attribute('username')->realm('My realm')
];
GuzzleHttp请求发生在后端,因此它不会像通常在浏览器中那样工作,例如提示输入用户名和密码.当您在不使用任何浏览器的情况下使用Guzzle时,您基本上是在模仿请求.因此,如果您需要提示,则必须在不使用GuzzleHttp的情况下实现此逻辑.
您仍然可以使用Guzzle进行测试,如下所示.
The below code will work.
$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
The below code will fail.
$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 Expressive
//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"
]
]));
上述两个代码都按预期工作.对于例如在浏览器中提示输入用户名和密码,并允许用户在发送正确的凭据时查看内容. Guzzle为你造成了问题.
我希望这有帮助.
内容总结
以上是互联网集市为您收集整理的php – 使用基本授权作为中间件PSR-7 PSR-15全部内容,希望文章能够帮你解决php – 使用基本授权作为中间件PSR-7 PSR-15所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。