【问题标题】:Laravel lcobucci JWT token issuing a tokenLaravel lcobucci JWT 代币发行代币
【发布时间】:2021-10-01 21:15:15
【问题描述】:

我正在尝试使用这个包来制作自定义格式的 jwt 令牌。

但我无法理解 $config = $container->get(Configuration::class);

这个 $container 变量是从哪里来的?

有人可以帮助我吗? 这是docs link

use Lcobucci\JWT\Configuration;

$config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$now   = new DateTimeImmutable();
$token = $config->builder()
                // Configures the issuer (iss claim)
                ->issuedBy('http://example.com')
                // Configures the audience (aud claim)
                ->permittedFor('http://example.org')
                // Configures the id (jti claim)
                ->identifiedBy('4f1g23a12aa')
                // Configures the time that the token was issue (iat claim)
                ->issuedAt($now)
                // Configures the time that the token can be used (nbf claim)
                ->canOnlyBeUsedAfter($now->modify('+1 minute'))
                // Configures the expiration time of the token (exp claim)
                ->expiresAt($now->modify('+1 hour'))
                // Configures a new claim, called "uid"
                ->withClaim('uid', 1)
                // Configures a new header, called "foo"
                ->withHeader('foo', 'bar')
                // Builds a new token
                ->getToken($config->signer(), $config->signingKey());
Once you've created a token, you're able to retrieve its data and convert it to its string representation:

use Lcobucci\JWT\Configuration;

$config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$token = $config->builder()
                ->issuedBy('http://example.com')
                ->withClaim('uid', 1)
                ->withHeader('foo', 'bar')
                ->getToken($config->signer(), $config->signingKey());

$token->headers(); // Retrieves the token headers
$token->claims(); // Retrieves the token claims

echo $token->headers()->get('foo'); // will print "bar"
echo $token->claims()->get('iss'); // will print "http://example.com"
echo $token->claims()->get('uid'); // will print "1"

echo $token->toString(); // The string representation of the object 

【问题讨论】:

    标签: laravel jwt


    【解决方案1】:

    就像打电话给Service Container

    在 Laravel 中,您可以使用 app() 帮助器创建 Configuration::class,但更好的方式 - 从构造函数注入

    通过助手

    $config = app()->make(Configuration::class);
    

    通过构造函数注入

    class JWTBuilderClass
    {
        private Configuration $configuration;
    
        public function __construct(Configuration $configuration)
        {
            $this->configuration = $configuration;
        }
    }
    

    在您从容器中获取配置之前 - 您必须将其放入容器中 - 绑定类。这是docs 关于配置创建的内容。在 AppServiceProvider::register():

    $this->app->bind(Configuration::class, function(){
        return Configuration::forAsymmetricSigner(
        // You may use RSA or ECDSA and all their variations (256, 384, and 512) and EdDSA over Curve25519
        new Signer\Rsa\Sha256(),
        LocalFileReference::file(__DIR__ . '/my-private-key.pem'),
           InMemory::base64Encoded('mBC5v1sOKVvbdEitdSBenu59nfNfhwkedkJVNabosTw=')
        // You may also override the JOSE encoder/decoder if needed by providing extra arguments here
        );
    });
    

    顺便说一句,使用包 tymondesigns/jwt-auth - 这是 Laravel 的 JWT 实现

    【讨论】:

    • 感谢您的帮助。但是在使用 $config = app()->make(Configuration::class); 之后我仍然无法使用此解决方案。我收到以下错误`目标 [Lcobucci\JWT\Configuration] 不可实例化。`
    猜你喜欢
    • 2020-02-18
    • 2018-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2023-03-12
    • 2020-06-01
    • 1970-01-01
    相关资源
    最近更新 更多