Slim 3 如何在本地存储上保存 JWT 令牌并在我的路由中使用它进行身份验证
Slim 3 how to save JWT Token on local storage and use it in my routes for authentication
我想为 slim 应用程序实现 jwt 身份验证,我遵循了 tuupora 的 PRS7 jwt 身份验证中间件,当我使用 Postman 时它工作正常,因为可以选择将 header 用作 "Authorization: Bearer tokenString",如下所示当我请求“/auth/ibice”路线时
these returned data are protected by the middleware-- screenshot
并且正在使用当我请求此路由“/authtoken”时返回的令牌字符串,如下所示
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3d3cuYXNpZC5ydyIsImlhdCI6MTQ4Njk5MjcyNCwiZXhwIjoxNDg4Mjg4NzI0LCJjb250ZXh0Ijp7InVzZXIiOnsicGhvbmVubyI6IjA3ODQyMjY4OTUiLCJ1c2VyX2lkIjoiMSJ9fX0.1kFu4A16xxJriaRA9CccIJ3M9Bup06buK2LAh13Lzy4",
"user_id": "1"
}
我的 middleware.php 保护“/auth/”的所有路由
<?php
// Application middleware
$container["jwt"] = function ($container) {
return new StdClass;
};
$app->add(new \Slim\Middleware\JwtAuthentication([
"environment" => "HTTP_X_TOKEN",
"header" => "Authorization",
"path" => ["/auth"],
"passthrough" => ["/authtoken"],
"secret" => "your_secret_key",
"error" => function ($request, $response, $arguments) {
$data["status"] = "error";
$data["message"] = $arguments["message"];
return $response->withStatus(401)
->withHeader("Content-Type", "application/json")
->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
},
"callback" => function ($request, $response, $arguments) use ($container) {
$container["jwt"] = $arguments["decoded"];
}
]));
以及我想通过授权 header 请求的路线,这些路线存储在 cookie 或本地存储中,但我不知道该怎么做!!
$app->group('/auth',function(){
$this->get('/admin','App\Controllers\apiController:login')->setName('admin');
//fetch ibice
$this->get('/ibice','App\Controllers\apiController:ibice')->setName('Ibice');
//fetch ibice by id
$this->get('/igice/{id}', 'App\Controllers\apiController:igice')->setName('igiceId');
//search ibice
$this->get('/igice/search/[{query}]', 'App\Controllers\apiController:igice_search')->setName('Igice Search');
//imitwe igize igice
$this->get('/igice/{id}/imitwe','App\Controllers\apiController:imitwe')->setName('Imitwe');
//ingingo ziherereye mumutwe runaka
$this->get('/umutwe/{id}/ingingo', 'App\Controllers\apiController:ingingoBundle')->setName('Ingingo.bundle');
//ingingo ziri mucyiciro runaka
$this->get('/ingingo/icyiciro/{id}', 'App\Controllers\apiController:allstuff')->setName('Icyiciro');
//kuzana ikibazo kimwe kiri mungingo runaka
$this->get('/ingingo/{ingingoid}/question/{id}', 'App\Controllers\apiController:question')->setName('One_Exercise');
//kuzana ibibazo byose biri mungingo
$this->get('/ingingo/{ingingoid}/questions', 'App\Controllers\apiController:questions')->setName('One_Exercise');
//check if the answer is True or False
$this->get('/question/{id}/check/[{query}]','App\Controllers\apiController:checkQuestions')->setName('Check_Questions');
//get questions ids from ingingo
$this->get('/question/{ingingoid}','App\Controllers\apiController:questionsIDs')->setName('Check_Questions');
});
请帮帮我,我不知道该怎么做!!
我以前从未使用过 Slim 但也许你可以使用很少的 Javascript 来访问本地存储 bcz 你不能使用 php 访问本地存储(php 在服务器端工作)虽然 localstorage 在浏览器(客户端)中,但您可以通过点击此 /authtoken 端点 $app->get('/authtoken') 来首先使用 php 获取 Auth 令牌,然后您需要 json_decode 将 json 返回到 php 数组中,然后如果假设包含令牌的 php 数组是 $arr 那么你可以 javascript 将令牌保存在本地存储中,就像这样 <script>localStorage.setItem('token', '<?php echo $arr['token'];?>');</script>
那么无论何时你想阅读它,你也可以使用 javascript 从 localstorage
中读取它
<?php
$token = "<script>document.write(localStorage.getItem('token'));</script>"; ?>
我想为 slim 应用程序实现 jwt 身份验证,我遵循了 tuupora 的 PRS7 jwt 身份验证中间件,当我使用 Postman 时它工作正常,因为可以选择将 header 用作 "Authorization: Bearer tokenString",如下所示当我请求“/auth/ibice”路线时 these returned data are protected by the middleware-- screenshot
并且正在使用当我请求此路由“/authtoken”时返回的令牌字符串,如下所示
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3d3cuYXNpZC5ydyIsImlhdCI6MTQ4Njk5MjcyNCwiZXhwIjoxNDg4Mjg4NzI0LCJjb250ZXh0Ijp7InVzZXIiOnsicGhvbmVubyI6IjA3ODQyMjY4OTUiLCJ1c2VyX2lkIjoiMSJ9fX0.1kFu4A16xxJriaRA9CccIJ3M9Bup06buK2LAh13Lzy4",
"user_id": "1"
}
我的 middleware.php 保护“/auth/”的所有路由
<?php
// Application middleware
$container["jwt"] = function ($container) {
return new StdClass;
};
$app->add(new \Slim\Middleware\JwtAuthentication([
"environment" => "HTTP_X_TOKEN",
"header" => "Authorization",
"path" => ["/auth"],
"passthrough" => ["/authtoken"],
"secret" => "your_secret_key",
"error" => function ($request, $response, $arguments) {
$data["status"] = "error";
$data["message"] = $arguments["message"];
return $response->withStatus(401)
->withHeader("Content-Type", "application/json")
->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
},
"callback" => function ($request, $response, $arguments) use ($container) {
$container["jwt"] = $arguments["decoded"];
}
]));
以及我想通过授权 header 请求的路线,这些路线存储在 cookie 或本地存储中,但我不知道该怎么做!!
$app->group('/auth',function(){
$this->get('/admin','App\Controllers\apiController:login')->setName('admin');
//fetch ibice
$this->get('/ibice','App\Controllers\apiController:ibice')->setName('Ibice');
//fetch ibice by id
$this->get('/igice/{id}', 'App\Controllers\apiController:igice')->setName('igiceId');
//search ibice
$this->get('/igice/search/[{query}]', 'App\Controllers\apiController:igice_search')->setName('Igice Search');
//imitwe igize igice
$this->get('/igice/{id}/imitwe','App\Controllers\apiController:imitwe')->setName('Imitwe');
//ingingo ziherereye mumutwe runaka
$this->get('/umutwe/{id}/ingingo', 'App\Controllers\apiController:ingingoBundle')->setName('Ingingo.bundle');
//ingingo ziri mucyiciro runaka
$this->get('/ingingo/icyiciro/{id}', 'App\Controllers\apiController:allstuff')->setName('Icyiciro');
//kuzana ikibazo kimwe kiri mungingo runaka
$this->get('/ingingo/{ingingoid}/question/{id}', 'App\Controllers\apiController:question')->setName('One_Exercise');
//kuzana ibibazo byose biri mungingo
$this->get('/ingingo/{ingingoid}/questions', 'App\Controllers\apiController:questions')->setName('One_Exercise');
//check if the answer is True or False
$this->get('/question/{id}/check/[{query}]','App\Controllers\apiController:checkQuestions')->setName('Check_Questions');
//get questions ids from ingingo
$this->get('/question/{ingingoid}','App\Controllers\apiController:questionsIDs')->setName('Check_Questions');
});
请帮帮我,我不知道该怎么做!!
我以前从未使用过 Slim 但也许你可以使用很少的 Javascript 来访问本地存储 bcz 你不能使用 php 访问本地存储(php 在服务器端工作)虽然 localstorage 在浏览器(客户端)中,但您可以通过点击此 /authtoken 端点 $app->get('/authtoken') 来首先使用 php 获取 Auth 令牌,然后您需要 json_decode 将 json 返回到 php 数组中,然后如果假设包含令牌的 php 数组是 $arr 那么你可以 javascript 将令牌保存在本地存储中,就像这样 <script>localStorage.setItem('token', '<?php echo $arr['token'];?>');</script>
那么无论何时你想阅读它,你也可以使用 javascript 从 localstorage
<?php
$token = "<script>document.write(localStorage.getItem('token'));</script>"; ?>