苗条的HttpBasicAuth

时间:2017-03-18 10:45:24

标签: php basic-authentication slim http-basic-authentication

我有单页REST应用程序,我正在尝试添加一些非常简单的授权。只需登录,无需注册。 我使用的是带有简单PDO身份验证器的HTTP Basic Authentication中间件,由Mika Tuupola提供。这是我的代码的一部分:

    require '../vendor/autoload.php';
    require '../src/config.php';

    $app = new Slim\App(['settings' => $config]);


    $container = $app->getContainer();
    $container['db'] = function ($c) {
    $db = $c['settings']['db'];
    $pdo = new PDO("mysql:host=" . $db['host'] . ";dbname=" . $db['dbname'],
        $db['user'], $db['pass']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
};


$app->add(new \Slim\Middleware\HttpBasicAuthentication([
    "path" => "/roomPictures",
    "realm" => "Protected",
    "authenticator" => new PdoAuthenticator([
        "pdo" => $pdo
    ])
]));


require '../src/routes.php';

$app->run();

重新加载mypage.dev/roomPictures后我得到500内部服务器错误。 你们有什么想法我可能做错了吗?

我不确定我是否理解这一点,但在触发/ roomPictures之后,系统对话窗口会出现询问用户名和密码的情况?

提前致谢!

1 个答案:

答案 0 :(得分:1)

在代码中添加以下行:

use Slim\Middleware\HttpBasicAuthentication\PdoAuthenticator;

或者将中间件实例化为:

$app->add(new Slim\Middleware\HttpBasicAuthentication([
    "path" => "/roomPictures",
    "realm" => "Protected",
    "authenticator" => new Slim\Middleware\HttpBasicAuthentication\PdoAuthenticator([
        "pdo" => $pdo
    ])
]));