fix : jwt
Some checks failed
Build Docker / build (push) Has been cancelled

This commit is contained in:
2025-09-13 12:16:13 +08:00
parent a1fdeb9148
commit 48ad2ebd1b
9 changed files with 135 additions and 18 deletions

View File

@@ -12,12 +12,20 @@ namespace App\Service\Admin\AdminUser;
use App\Lib\Jwt\RequestScopedTokenTrait;
use App\Service\Admin\BaseAdminService;
use App\Service\BaseTokenService;
use Hyperf\Di\Annotation\Inject;
use Lcobucci\JWT\Token\RegisteredClaims;
class UserService extends BaseAdminService
{
use RequestScopedTokenTrait;
/**
* @var BaseTokenService
*/
#[Inject]
protected BaseTokenService $tokenService;
public function handle(): array
{
var_dump($this->getToken()->claims()->all());
@@ -27,8 +35,13 @@ class UserService extends BaseAdminService
return $this->adminReturn->success();
}
public function refresh(): array
/**
* @return array
*/
public function logout(): array
{
$this->tokenService->setJwt('admin')->getJwt()->addBlackList($this->getToken());
return $this->adminReturn->success();
}
}

View File

@@ -21,11 +21,6 @@ use Hyperf\Di\Annotation\Inject;
class LoginService extends BaseAdminService
{
/**
* @var string jwt场景
*/
private string $jwt = 'admin';
/**
* @var AdminUserRepository
*/
@@ -54,7 +49,7 @@ class LoginService extends BaseAdminService
if ($adminInfo->status == AdminUserStatusCode::DISABLE) throw new ErrException('用户已禁用');
$jwtHandle = $this->tokenService->getJwt();
$jwtHandle = $this->tokenService->setJwt('admin')->getJwt();
return $this->adminReturn->success('success',[
'access_token' => $jwtHandle->builderAccessToken((string) $adminInfo->id)->toString(),

View File

@@ -10,18 +10,43 @@ declare(strict_types=1);
namespace App\Service\Admin\Login;
use App\Lib\Jwt\RequestScopedTokenTrait;
use App\Service\Admin\BaseAdminService;
use App\Service\BaseTokenService;
use Hyperf\Di\Annotation\Inject;
use Lcobucci\JWT\Token\RegisteredClaims;
use Lcobucci\JWT\UnencryptedToken;
class RefreshService extends BaseAdminService
{
use RequestScopedTokenTrait;
/**
* @var BaseTokenService
*/
#[Inject]
protected BaseTokenService $tokenService;
/**
* @return array
*/
public function handle(): array
{
return $this->adminReturn->success();
return $this->adminReturn->success('success',$this->refreshToken($this->getToken()));
}
public function refreshToken(UnencryptedToken $token)
/**
* @param UnencryptedToken $token
* @return array<string,int|string>
*/
public function refreshToken(UnencryptedToken $token): array
{
$jwt = $this->tokenService->setJwt('admin')->getJwt();
$jwt->addBlackList($token);
return [
'access_token' => $jwt->builderAccessToken($token->claims()->get(RegisteredClaims::ID))->toString(),
'refresh_token' => $jwt->builderRefreshToken($token->claims()->get(RegisteredClaims::ID))->toString(),
'expire_at' => (int) $jwt->getConfig('ttl', 0),
];
}
}

View File

@@ -46,6 +46,16 @@ final class BaseTokenService implements CheckTokenInterface
$this->getJwt()->hasBlackList($token) && throw new ErrException('token已过期');
}
/**
* @param string $jwt
* @return $this
*/
public function setJwt(string $jwt): self
{
$this->jwt = $jwt;
return $this;
}
/**
* @param UnencryptedToken $token
* @return \Closure