Skip to content

Commit f55e45a

Browse files
committed
修复 刷新有效期问题
1 parent 83051e1 commit f55e45a

3 files changed

Lines changed: 16 additions & 16 deletions

File tree

config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'signerKey' => '',
77
'notBefore' => 0,
88
'expiresAt' => 3600,
9-
'refreshExp' => 7200,
9+
'refreshTTL' => 7200,
1010
'signer' => 'Lcobucci\JWT\Signer\Hmac\Sha256',
1111
'type' => 'Header',
1212
'refresh' => 50001,

src/Jwt.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public function token(array $claims): Token
7878
->setId($uniqid, true)
7979
->setIssuedAt(time())
8080
->setNotBefore(time() + $this->notBefore())
81-
->setExpiration(time() + $this->ttl());
81+
->setExpiration(time() + $this->ttl())
82+
->set('refreshAt', time() + $this->refreshTTL());
8283

8384
foreach ($claims as $key => $claim) {
8485
$this->builder->set($key, $claim);
@@ -216,6 +217,11 @@ public function verify(Token $token = null)
216217
*/
217218
protected function validateToken()
218219
{
220+
// 是否在黑名单
221+
if ($this->manager->hasBlacklist($this->token)) {
222+
throw new TokenAlreadyEexpired('此 Token 已注销,请重新登录', $this->getReloginCode());
223+
}
224+
219225
// 验证密钥是否与创建签名的密钥一致
220226
if (false === $this->token->verify($this->getSigner(), $this->makeSignerKey())) {
221227
throw new JWTException('此 Token 与 密钥不匹配', 500);
@@ -227,18 +233,13 @@ protected function validateToken()
227233
throw new JWTException('此 Token 暂未可用', 500);
228234
}
229235

230-
// 是否在黑名单
231-
if ($this->manager->hasBlacklist($this->token)) {
232-
throw new TokenAlreadyEexpired('此 Token 已注销,请重新登录', $this->getReloginCode());
233-
}
234-
235236
// 是否已过期
236-
if ($this->token->isExpired()) {
237-
if (time() < ($this->token->getClaim('iat') + $this->refreshTTL())) {
238-
throw new TokenAlreadyEexpired('Token 已过期,请重新刷新', $this->getAlreadyCode());
239-
} else {
240-
throw new TokenAlreadyEexpired('Token 刷新时间已过,请重新登录', $this->getReloginCode());
237+
if (true === $this->token->isExpired()) {
238+
if (time() <= $this->token->getClaim('refreshAt')) {
239+
throw new TokenAlreadyEexpired('Token 已过期,请重新刷新' .time() . '-' . $this->token->getClaim('refreshAt'), $this->getAlreadyCode());
241240
}
241+
242+
throw new TokenAlreadyEexpired('Token 刷新时间已过,请重新登录', $this->getReloginCode());
242243
}
243244

244245
$data = new ValidationData();

src/Traits/Jwt.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ trait Jwt
2121

2222
private $type = 'Header';
2323

24-
private $hasLogged = 50401;
25-
private $tokenAlready = 50402;
26-
private $relogin = 50400;
24+
private $refresh = 50001;
25+
private $relogin = 50002;
2726

2827
private $iss;
2928
private $aud;
@@ -137,7 +136,7 @@ public function setTTL(int $value)
137136
*/
138137
public function getAlreadyCode()
139138
{
140-
return $this->tokenAlready;
139+
return $this->refresh;
141140
}
142141

143142
/**

0 commit comments

Comments
 (0)