中间件实现HTTP基本访问认证 https://en.wikipedia.org/wiki/Basic_access_authentication。身份验证对话框通过响应标头触发。由浏览器供应商决定如何询问凭据。大多数浏览器使用您描述的弹出登录对话框。
您正在尝试做的是使用 HTTP 基本身份验证的一种非正统方式。但是,您可以通过删除来抑制登录对话框WWW-Authenticate
响应中的标头。请注意您需要的至少版本2.0.2 https://packagist.org/packages/tuupola/slim-basic-auth为了这个工作。
$app->add(new \Slim\Middleware\HttpBasicAuthentication([
"path" => ["/main"],
"authenticator" => new PdoAuthenticator([
"pdo" => $pdo,
"table" => "accounts",
"user" => "accountUsername",
"hash" => "accountPassword"
]),
"error" => function ($request, $response, $arguments) {
return $response
->withRedirect("/auth/login")
->withoutHeader("WWW-Authenticate");
}
]));
但是,使用上面的代码,您仍然需要设置Authentication: Basic
以某种方式请求标头。一种方法是使用 AJAX 请求。
$.ajax({
url: "http://example.com/auth/login",
username: $("username").val(),
password: $("password").val(),
success: function(result) {
alert("Authorization header should now be set...");
}
});