Slim框架:无法使用JWT标记。

问题描述 投票:0回答:1

我正在做一个纤细的REST API,我想用JWT Token来保护它。我尝试了很多教程,我可以使事情工作。

我使用: slim 4.* slimpsr7 0.6.0 tuupolaslim-jwt-auth ^3.4 tuupolacors-middleware ^1.1。

Ubuntu 19.10和Xampp

我有2个路由(POST登录和GET apitest),我希望能够使用登录路由不需要令牌,而另一个则需要令牌,所以我写了.POST登录和GET apitest。

$app->add(new Tuupola\Middleware\JwtAuthentication([
    "path" => "/api",
    "secret" => getenv ("SPROUTCH_TOKEN"),
    "error" => function ($request, $response, $arguments) {
        $data["status"] = "error";
        $data["message"] = $arguments["message"];
        return $response
            ->withHeader("Content-Type", "application/json")
            ->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
    },
]));

在这种情况下,没有什么是安全的,所以我试了一下。

$app->add(new Tuupola\Middleware\JwtAuthentication([
    "secret" => getenv ("SPROUTCH_TOKEN"),
    "error" => function ($request, $response, $arguments) {
        $data["status"] = "error";
        $data["message"] = $arguments["message"];
        return $response
            ->withHeader("Content-Type", "application/json")
            ->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
    },
]));

当然,我不能访问任何东西。

php authentication composer-php jwt slim
1个回答
0
投票

问题是 "路径 "键只需要绝对路径。

© www.soinside.com 2019 - 2024. All rights reserved.