简体   繁体   English

访问 JWT 负载变量(Slim 框架中的 tuupola)

[英]Access JWT payload variables (tuupola in Slim Framework)

I am successfully authenticating my users against my API using Slim Framework 4. I do so using a middle ware that looks like this:我使用 Slim Framework 4 成功地针对我的 API 对我的用户进行了身份验证。我使用如下所示的中间件进行身份验证:

$app->add(new \Tuupola\Middleware\JwtAuthentication([

    "algorithm" => ["HS512"],
    "logger" => $logger,
    "secret" => getenv('JWT_SECRET'),
    "rules" => [
        new Tuupola\Middleware\JwtAuthentication\RequestPathRule([
            "path" => "/v2"
        ])
    ]
]));

This works like a breeze except one thing.除了一件事之外,这就像微风一样。 The payload contains a string that I need to pass to the database so I need to read the token in the API when it is sent back to the API (It is in the http headers).有效负载包含一个我需要传递给数据库的字符串,因此当它被发送回 API 时我需要读取 API 中的令牌(它在 http 标头中)。

How could I automatically set a PHP variable that contains the string coming from the payload?如何自动设置包含来自有效负载的字符串的 PHP 变量?

The token arrives back to the API in the Authorization header.令牌返回到 Authorization 标头中的 API。

The payload when sent back to the API looks like this:发送回 API 时的负载如下所示:

{
  "jti": "1PD0vKUwqlBocToxmZgeJA",
  "iat": 1580575309,
  "exp": 1580577109,
  "stk": "KsdL5WxiGkYIkFeduiQkMR8nTwOZ56ik"
}

I am interrested in accessing the value of "stk" :)我对访问“stk”的值很感兴趣:)

I am not an advanced PHP developer so please forgive me if my question is a bit simple.我不是高级 PHP 开发人员,所以如果我的问题有点简单,请原谅我。

Thanks a lot in advance for your precious answers.非常感谢您的宝贵回答。

So,所以,

The answer is Here .答案在这里

It is passed as a attribute and easily accessed as any attribute later on.它作为属性传递,以后可以作为任何属性轻松访问。

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

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