【发布时间】:2020-04-19 09:42:23
【问题描述】:
我正在开发一个精简的 REST API,我想使用 JWT 令牌来保护它。我尝试了很多教程,我能够让事情发挥作用。
我使用:
纤薄 4.*
苗条/psr7 0.6.0
tuupola/slim-jwt-auth ^3.4
tuupola/cors 中间件 ^1.1
Ubuntu 19.10 和 Xampp
我有 2 条路线(POST /login 和 GET /api/test)
我希望能够使用没有令牌的 /login 路由,而另一个有令牌的路由。
所以我写了:
$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