response = $response; $this->request = $request; $this->jwt = $jwt; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $isValidToken = false; // 获取请求token $token = $request->getHeaderLine('Authorization'); if (empty($token)) { $token = $this->request->input('token', ''); } if (!empty($token)) { try { $token = JWTUtil::handleToken($token); if ($token !== false && $this->jwt->checkToken($token)) { $isValidToken = true; } } catch (\Exception $e) { $isValidToken = false; } } if (!$isValidToken) { return $this->response->withStatus(401)->json([ 'code' => 401, 'message' => 'Token authentication does not pass', ]); } return $handler->handle($request); } }