更新到版本 10.1.0 后,我现在在另一个验证 jwt token 的服务中遇到问题。我注意到 token 值已更改为
"iat": 1600409130,
"nbf": 1600409130,
"exp": 1631945129
至
"iat": "1607005988.812500",
"nbf": "1607005988.812513",
"exp": "1638541988.293214",
有人知道如何去掉这里的小数吗?
最佳答案
更改 Passport 处的 ClaimsFormatter ^10
第1步.创建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());
}
}
第2步.创建AccessTokenRepository类
<?php
namespace App\Repositories;
use App\Passport\AccessToken;
use Laravel\Passport\Bridge\AccessTokenRepository as BaseRepository;
use League\OAuth2\Server\Entities\ClientEntityInterface;
class AccessTokenRepository extends BaseRepository {
public function getNewToken( ClientEntityInterface $clientEntity, array $scopes, $userIdentifier = null ) {
return new AccessToken( $userIdentifier, $scopes, $clientEntity );
}
}
第3步.在config/app.php中添加提供程序
'providers' => [
...
/*
* Application Service Providers...
*/
App\Providers\PassportServiceProvider::class,
],
第4步.创建提供者类
<?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()
);
}
}
第 5 步:创建 ClaimsFormatter 类
<?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');
}
}
关于Laravel Passport 将 exp、iat 和 nbf 类型更改为 int 或 float,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65129092/