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