controller); $noNeedLogin = $controller->getDefaultProperties()['noNeedLogin'] ?? []; if (in_array($request->action, $noNeedLogin)) { // 白名单 return $handler($request); } else { try { $authHeader = $request->header('Authorization'); if (!$authHeader || !preg_match('/Bearer\s(\S+)/', $authHeader, $matches)) { $body = [ 'status' => 'fail', 'code' => 401, 'message' => '未授权', 'data' => [], ]; return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']); } $token = $matches[1]; $request->user = (new Jwt())->verifyToken($token); } catch (\Throwable $e) { $body = [ 'status' => 'fail', 'code' => 401, 'message' => $e->getMessage(), 'data' => [], ]; return response(json_encode($body, JSON_UNESCAPED_UNICODE), 401, ['Content-Type' => 'application/json']); } return $handler($request); } } }