1

我使用来自 Slim/PHP 的 HttpBasicAuthentication 来保护对我的“/login”路由的访问,在验证访问之后,将创建一个 JWT 以授予对所有路由的访问权限。所以我想用数据库中的用户个人资料信息生成一个自定义 jwt,但我无法获取用户信息……所有参数都是空的。如何解决?

$app->add(new \Tuupola\Middleware\HttpBasicAuthentication([ 
    "path" => "/login",
    "realm" => "Protected",
    "authenticator" => new LoginAuthenticator(),
    "error" => function ($response, $arguments) {
        $data = [];
        $data["status"] = "error";
        $data["message"] = $arguments["message"];

        $body = $response->getBody();
        $body->write(json_encode($data, JSON_UNESCAPED_SLASHES));

        return $response->withBody($body);
    },    
    "before" => function ($request, $arguments) {
        return $request->withAttribute("user", $arguments["user"]);
    }
]));

路线

$app->get('/login', function (Request $request, Response $response) use ($app) {

    $params = (object) $request->getParams()
    $key = $this->get("secretkey");

    $token = array(
        "user" => $params->user,
        "email" => $params->email,
        "age" => $params->age
    );

    $jwt = JWT::encode($token, $key);  

    return $response->withJson(["jwt" => $jwt], 200)
        ->withHeader('Content-type', 'application/json');

});
4

1 回答 1

1

如果你有 $token , $key, algorithm ,你可以用代码来检索有效载荷

JWT::decode($token, $key, array(‘HS256’));
于 2020-09-28T18:58:53.700 回答