Laravel Passport将exp、iat和nbf类型更改为int或float

Laravel Passport将exp、iat和nbf类型更改为int或float,laravel,laravel-passport,Laravel,Laravel Passport,在更新到版本10.1.0之后,我现在在另一个验证jwt令牌的服务中遇到了一个问题。我注意到令牌值从 “iat”:1600409130, “nbf”:1600409130, “经验”:1631945129 到 “iat”:“1607005988.812500”, “nbf”:“1607005988.812513”, “exp”:“1638541988.293214”, 有人知道如何删除这里的小数吗?在Passport ^10更改索赔格式 第一步。生成AccessToken类 <?php

在更新到版本10.1.0之后,我现在在另一个验证jwt令牌的服务中遇到了一个问题。我注意到令牌值从

“iat”:1600409130,
“nbf”:1600409130,
“经验”:1631945129

“iat”:“1607005988.812500”,
“nbf”:“1607005988.812513”,
“exp”:“1638541988.293214”,

有人知道如何删除这里的小数吗?

在Passport ^10更改索赔格式

第一步。生成AccessToken类

 <?php

namespace App\Passport;


use DateTimeImmutable;
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\Key\LocalFileReference;
use League\OAuth2\Server\CryptKey;
use Lcobucci\JWT\Signer\Rsa\Sha256;
use Laravel\Passport\Bridge\AccessToken as BaseToken;

class AccessToken extends BaseToken {

    /**
     * @var CryptKey
     */
    private $privateKey;

    /**
     * @var Configuration
     */
    private $jwtConfiguration;

    /**
     * Set the private key used to encrypt this access token.
     */
    public function setPrivateKey( CryptKey $privateKey ) {
        $this->privateKey = $privateKey;
    }

    /**
     * Generate a string representation from the access token
     */
    public function __toString()
    {
        return $this->convertToJWT()->toString();
    }


    /**
     * Initialise the JWT Configuration.
     */
    public function initJwtConfiguration()
    {
        $this->jwtConfiguration = Configuration::forAsymmetricSigner(
            new Sha256(),
            LocalFileReference::file($this->privateKey->getKeyPath(), $this->privateKey->getPassPhrase() ?? ''),
            InMemory::plainText('')
        );
    }


    public function convertToJWT() {
        $this->initJwtConfiguration();

        return $this->jwtConfiguration->builder(new MicrosecondBasedDateConversion())
            ->permittedFor($this->getClient()->getIdentifier())
            ->identifiedBy($this->getIdentifier())
            ->issuedAt(new DateTimeImmutable())
            ->canOnlyBeUsedAfter(new DateTimeImmutable())
            ->expiresAt($this->getExpiryDateTime())
            ->relatedTo((string) $this->getUserIdentifier())
            ->withClaim('scopes', $this->getScopes())
            ->withClaim('test',[])
            ->getToken($this->jwtConfiguration->signer(), $this->jwtConfiguration->signingKey());
    }
}
第四步。生成提供程序类

<?php

namespace App\Providers;

use App\Repositories\AccessTokenRepository;
use Laravel\Passport\Bridge\ClientRepository;
use Laravel\Passport\Bridge\ScopeRepository;
use League\OAuth2\Server\AuthorizationServer;

class PassportServiceProvider extends \Laravel\Passport\PassportServiceProvider {
  
    public function makeAuthorizationServer() {
    
        return new AuthorizationServer(
            $this->app->make( ClientRepository::class ),
            $this->app->make( AccessTokenRepository::class ),
            $this->app->make( ScopeRepository::class ),
            $this->makeCryptKey( 'private' ),
            app( 'encrypter' )->getKey()
        );
    }

}

'providers' => [
       ...
       
    /*
    * Application Service Providers...
    */
      
    App\Providers\PassportServiceProvider::class,
 ],
<?php

namespace App\Providers;

use App\Repositories\AccessTokenRepository;
use Laravel\Passport\Bridge\ClientRepository;
use Laravel\Passport\Bridge\ScopeRepository;
use League\OAuth2\Server\AuthorizationServer;

class PassportServiceProvider extends \Laravel\Passport\PassportServiceProvider {
  
    public function makeAuthorizationServer() {
    
        return new AuthorizationServer(
            $this->app->make( ClientRepository::class ),
            $this->app->make( AccessTokenRepository::class ),
            $this->app->make( ScopeRepository::class ),
            $this->makeCryptKey( 'private' ),
            app( 'encrypter' )->getKey()
        );
    }

}
<?php

namespace App\Passport;

use DateTimeImmutable;
use Lcobucci\JWT\ClaimsFormatter;
use Lcobucci\JWT\Token\RegisteredClaims;

class MicrosecondBasedDateConversion implements ClaimsFormatter
{
    /** @inheritdoc */
    public function formatClaims(array $claims): array
    {
        foreach (RegisteredClaims::DATE_CLAIMS as $claim) {
            if (! array_key_exists($claim, $claims)) {
                continue;
            }

            $claims[$claim] = $this->convertDate($claims[$claim]);
        }

        return $claims;
    }

    /**
     * @param DateTimeImmutable $date
     * @return int
     */
    private function convertDate(DateTimeImmutable $date): int
    {
        return (int)$date->format('U');
    }
}