我想为slim app实现jwt身份验证,我遵循tuupora的PRS7 jwt身份验证中间件,并且当我使用Postman时它的工作正常,因为有选项使用标题作为“授权:Bearer tokenString”,如下所示,当我请求“/ auth / ibice“路线 these returned data are protected by the middleware-- screenshot
并且正在使用我请求此路由时返回的令牌字符串“/ authtoken”,如下所示
{
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ3d3cuYXNpZC5ydyIsImlhdCI6MTQ4Njk5MjcyNCwiZXhwIjoxNDg4Mjg4NzI0LCJjb250ZXh0Ijp7InVzZXIiOnsicGhvbmVubyI6IjA3ODQyMjY4OTUiLCJ1c2VyX2lkIjoiMSJ9fX0.1kFu4A16xxJriaRA9CccIJ3M9Bup06buK2LAh13Lzy4",
"user_id": "1"
}
这是我的中间件.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"];
}
]));
以及我想要使用授权标头请求的路由,该标头存储在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');
});
请帮帮我,我不知道怎么做!!
答案 0 :(得分:0)
我之前从未使用过Slim但也许你可以使用小Javascript来访问localstorage bcz你无法使用php访问本地存储(php在服务器端工作)而localstorage在浏览器(客户端)这里有步骤你可以首先通过点击此/ authtoken端点$ app-&gt; get('/ authtoken')获取带有php的Auth令牌然后你需要将json_decode返回json进入php数组然后如果假设你的php数组包含令牌是$ arr然后你就可以了在localstorage中保存该令牌的小javascript喜欢这个<script>localStorage.setItem('token', '<?php echo $arr['token'];?>');</script>
然后每当你想要阅读它时你也可以使用javascript从localstorage中读取它
<?php
$token = "<script>document.write(localStorage.getItem('token'));</script>"; ?>