修正解析token时过期时处理
This commit is contained in:
parent
2aa253fa5d
commit
17c979105c
@ -62,9 +62,9 @@ class AuthMiddleware implements MiddlewareInterface
|
||||
$white_api = $Auth->checkApiWhiteList($path_info, $method);
|
||||
|
||||
if (!empty($token)){
|
||||
$res = $redis->get('jwt_black_' . $token);
|
||||
if ($white_api){
|
||||
// 存在token,免鉴权
|
||||
$res = $redis->get('jwt_black_' . $token);
|
||||
if ($res && time() >= $res) {
|
||||
// token存在黑名单中
|
||||
return $handler->handle($request);
|
||||
@ -78,31 +78,37 @@ class AuthMiddleware implements MiddlewareInterface
|
||||
}
|
||||
}else{
|
||||
// 存在token,鉴权
|
||||
$res = $redis->get('jwt_black_' . $token);
|
||||
if ($res && time() >= $res) {
|
||||
// token存在黑名单中
|
||||
return $this->response->json(fail(HttpEnumCode::TOKEN_ERROR));
|
||||
}
|
||||
|
||||
// jwt验证
|
||||
$result = $Jwt->decode($token);
|
||||
try {
|
||||
// jwt验证
|
||||
$result = $Jwt->decode($token);
|
||||
|
||||
// 处理即将过期token
|
||||
$req = $Auth->checkTokenExpTime($result);
|
||||
if ($req) {
|
||||
// 即将过期,重新下发token
|
||||
$new_token = $Jwt->encode($result['userInfo']);
|
||||
// 处理即将过期token
|
||||
$req = $Auth->checkTokenExpTime($result);
|
||||
if ($req) {
|
||||
// 即将过期,重新下发token
|
||||
$new_token = $Jwt->encode($result['userInfo']);
|
||||
|
||||
// 旧token加入黑名单 5天有效期,5天内,无法继续进行访问
|
||||
$res = $redis->set('jwt_black_' . $token, $result['exp'], 30);
|
||||
if (!$res) {
|
||||
// 添加缓存失败
|
||||
return $this->response->json(fail(HttpEnumCode::SERVER_ERROR));
|
||||
// 旧token加入黑名单 5天有效期,5天内,无法继续进行访问
|
||||
$res = $redis->set('jwt_black_' . $token, $result['exp'], 30);
|
||||
if (!$res) {
|
||||
// 添加缓存失败
|
||||
return $this->response->json(fail(HttpEnumCode::SERVER_ERROR));
|
||||
}
|
||||
|
||||
$response = Context::get(ResponseInterface::class);
|
||||
$response = $response->withHeader('Authorization', $new_token);
|
||||
Context::set(ResponseInterface::class, $response);
|
||||
}
|
||||
|
||||
$response = Context::get(ResponseInterface::class);
|
||||
$response = $response->withHeader('Authorization', $new_token);
|
||||
Context::set(ResponseInterface::class, $response);
|
||||
}catch (\Throwable $e){
|
||||
if ($e->getCode() == 405 || $e->getCode() == 406){
|
||||
return $this->response->json(fail($e->getCode()));
|
||||
}
|
||||
return $this->response->json(fail(HttpEnumCode::SERVER_ERROR));
|
||||
}
|
||||
}
|
||||
}else{
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user