简体   繁体   English

Slim 框架:无法使用 JWT 令牌

[英]Slim Framework : unable to use JWT token

I am working on a slim REST API and I want to secure it with JWT Token.我正在开发一个纤薄的 REST API,我想用 JWT 令牌来保护它。 I try a lot of tutorials and I am anable to make things work.我尝试了很多教程,我能够使事情发挥作用。

I use :我用 :
slim 4.*苗条 4.*
slim/psr7 0.6.0超薄/psr7 0.6.0
tuupola/slim-jwt-auth ^3.4 tuupola/slim-jwt-auth ^3.4
tuupola/cors-middleware ^1.1 tuupola/cors-中间件 ^1.1

Ubuntu 19.10 and Xampp Ubuntu 19.10 和 Xampp

I have have 2 routes (POST /login and GET /api/test)我有 2 条路线(POST /login 和 GET /api/test)
I want to be able to use the /login route without token and the other one with a token.我希望能够在没有令牌的情况下使用 /login 路由,而使用另一个带有令牌的路由。 So I wrote :所以我写道:

$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));
    },
]));

In that case nothing is secure so I tried this :在那种情况下,没有什么是安全的,所以我尝试了这个:

$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));
    },
]));

And of course I can't access anything.当然,我无法访问任何内容。

问题只是“路径”键只需要绝对路径

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM