mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 15:57:50 +08:00
fix : jwt
Some checks failed
Build Docker / build (push) Has been cancelled
Some checks failed
Build Docker / build (push) Has been cancelled
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
|
||||
@@ -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),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user