【发布时间】:2017-02-26 01:41:35
【问题描述】:
我正在使用带有 JWT 的 Slim v 3 来编写 REST API。我关注了https://github.com/tuupola/slim-jwt-auth,它工作正常。
每次用户登录应用程序时,我都会生成一个令牌。为了对用户进行身份验证,我按照https://github.com/tuupola/slim-basic-auth 将其用作身份验证中间件。成功后,我正在使用https://github.com/firebase/php-jwt 生成一个令牌。
我在这里遇到了一个关于 SO 的相关问题,JWT: Authentication in slim v3 and Android,我有一个关于 http 基本身份验证的查询。 (我没有足够的代表在那里发表评论)。
现在我的问题:
通过 'users' 选项的 HttpBasicAuthentication 工作正常,但显然我无法针对我的用户表使用它。许多用户将登录到应用程序并在“用户”中列出所有用户不是一个选项。我在吗?
如果是,我必须使用 Pdo Authenticator。我配置了它,但身份验证失败,我无法解决它。使用“身份验证失败”消息触发错误回调。我的数据库有 'users' 表,其中包含用户名和密码的 'user' 和 'hash' 列。下面是我正在使用的一段代码。
use Slim\Middleware\HttpBasicAuthentication; use Slim\Middleware\HttpBasicAuthentication\PdoAuthenticator;
$pdo = new \PDO('mysql:host=localhost;dbname=test', $dbUser, $dbPassword);
$middlewareHttpBasicAuthConfig = [
/*"users" => [
"user1" => "password"
],*/
"secure" => false,
"relaxed" => ["localhost", "amruta-pani"],
"path" => "/*",
"passthrough" => Utils::httpAuthPassThroughRoutes,
"realm" => "Protected",
"authenticator" => new PdoAuthenticator([
"pdo" => $pdo
]),
"callback" => function($request, $response, $arguments) {
echo "Through<br>\n";
print_r($arguments);
},
"error" => function($request, $response, $arguments) {
echo "Failed<br>\n";
print_r($arguments);
}
];
$app->add(new HttpBasicAuthentication($middlewareHttpBasicAuthConfig));
我正在使用 Google Advanced Rest Client 对其进行测试,我看到的输出是
Failed<br>
Array
(
[message] => Authentication failed
)
我已将以下规则添加到我的 Apache 网络服务器
RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
我在这里错过了什么?
【问题讨论】:
-
您可以添加指向您正在使用的 PDO Authenticatior 的链接吗?
-
@GeorgyIvanov 我关注了他们两个:github.com/tuupola/slim-basic-auth#usage-with-pdo,appelsiini.net/2014/slim-database-basic-authentication。第二个是关于PDO的详细介绍
-
可以连接数据库吗?您是如何将用户插入数据库的,即您在将密码插入数据库之前是否对密码进行了哈希处理?
-
是的,我可以连接到数据库。但是我在测试时没有散列密码。是否需要对其进行哈希处理?
-
是的,仅限散列密码。如果您想使用明文密码,您必须按照 Werner 的建议编写自己的身份验证器。
标签: php pdo slim basic-authentication