【问题标题】:SLIM BasicAuth no workSLIM BasicAuth 无效
【发布时间】:2014-11-23 19:07:12
【问题描述】:

对不起我的英语。

我为我的新项目 API REST 测试 SLIM 框架。

我使用证书 SSL 在我的 Apache CGI 服务器上在线安装了 SLIM:好的,它可以工作!我可以从我的电脑访问我的资源(例如https://domaine.fr/v1/test

我通过 htaccess 文件使用基本身份验证进行了测试。我录制用户/密码以访问我的资源:它有效!

现在,我想在 SLIM 中使用 https://github.com/tuupola/slim-basic-auth 进行基本身份验证测试

但它不起作用!它一直在问我登录名和密码!

我的 htaccess 文件:

Satisfy any
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]

我的 index.php(苗条):

require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();

$app->add(new \Slim\Middleware\HttpBasicAuthentication(array(
    "realm" => "Here be dragons.",
    "users" => array(
        "root" => "t00r",
        "test" => "test"
    )
)));

我不明白为什么它不起作用。

谁能帮帮我?

谢谢

【问题讨论】:

  • 浏览器询问时您输入的用户名和密码是什么?
  • 我使用“root/t00r”或“test/test”。

标签: authentication slim


【解决方案1】:

刚刚测试过,这里有适合您的工作代码。请注意,由于您正在使用 composer 安装依赖项,因此使用 composer 自动加载是一个好主意。

require_once "../vendor/autoload.php";

$app = new \Slim\Slim();

$app->add(new \Slim\Middleware\HttpBasicAuthentication(array(
    "realm" => "Here be dragons.",
    "users" => array(
        "root" => "t00r",
        "test" => "test"
    )
)));

$app->get("/", function() {
    print "Foo";
});

$app->run();

【讨论】:

  • 您的服务器可能在 CGI 模式下运行 PHP。请参阅 documentation 关于 FastCGI 的使用。
  • 感谢您的帮助!它现在正在使用 FastCGI 和您的文档。
【解决方案2】:

对不起,我的回复晚了,我从 Composer/Slim 开始,我花了一个不眠之夜在我的计算机上进行本地测试(Windows 7)。我安装了 Composer for Windows、Slim 和 Composer 以及 tuupola/slim-basic-auth 和 Composer。我创建了两条路线,一条公共路线,一条私人路线。它在本地工作:“http://mycomputer/ws-bots”和“http://mycomputer/ws-bots/admin”,我输入“test/test”并显示“admin”消息。

所以我在我的 VPS Linux (Centos) 上进行了两种测试:

第一种方式)我在 /httpdocs/ws-bots/v2/ 中复制了 filezilla,所以我有:

- Vendor folder,
- Composer.json,
- composer.lock
- And composer.phar

在vendor文件夹中,有:

- Compose folder,
- Ircmaxell folder,
- Slim folder,
- Tuupola folder,
- And autoload.php

我创建了一个虚拟主机:https://ws2-bots.faistescourses.fr => /httpdocs/ws-bots/v2/vendor/Slim/slim: 好的

我在 index.php "/var/www/vhosts/faistescourses.fr/httpdocs/ws-bots/v2/vendor/autoload.php" 中更改了自动加载路径:好的

$app-> add (new \ Slim \ Middleware \ HttpBasicAuthentication (array (
"Path" => "/ admin"
"realm" => "Private"
"users" => array (
"root" => "t00r"
"test" => "test"
)
)));

我可以访问:https://ws2-bots.faistescourses.fr:好的

$ app-> get ('/', function () {
print "Public";
});

如果我测试https://ws2-bots.faistescourses.fr/admin:不工作

$ app-> get ("/ admin", function () {
print "Private";
});

我输入“root/t00r”或“test/test”作为登录名/密码:它不起作用!他让我输入登录名/密码。

如果我使用 /* .. */ 禁用身份验证,我可以访问 /admin。

第二种方式)我已经在我的 VPS、Composer、带有 Composer 的 Slim 和带有 Composer 的 tuupola/slim-basic-auth 上安装了“/var/www/vhosts/faistescourses.fr/httpdocs/ws-bots/v2/ 。”我如上所述更改了路径和 autoload.php index.php。 我也有同样的问题。如果我使用 /../ 禁用身份验证,它可以工作。

所以我不明白,我在其他地方有问题吗?路径?

您可以使用“root/t00r”或“test/test”测试 https://ws2-bots.faistescourses.fr 和 https://ws2-bots.faistescourses.fr/admin。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多