CodeIgniter3本地Google OAuth登录正常,队友机器跳转卡顿问题排查
CodeIgniter 3 Google OAuth登录本地正常,队友机器重定向卡顿问题
我在CodeIgniter 3项目中实现了Google OAuth登录功能,本地运行完全正常,但队友运行相同代码时出现异常:登录后应用重定向至https://localhost/source_web/ctm/login?code=...,流程卡顿无法完成登录。
已执行的排查步骤
- 验证重定向URI:确认Google API控制台中的重定向URI与应用使用的一致;
- 检查CORS问题:确认Google API客户端允许重定向至队友机器,且浏览器控制台无CORS相关错误;
- 检查Google账号权限:确认所用Google账号已启用OAuth应用所需权限。
代码片段
加载Google配置函数
function loadGoogleLoginInfo($clientId, $clientSecret, $redirectUri) { // Create Client Request to access Google API $client = new Google_Client(); $client->setApplicationName("PHP Google OAuth Login Example"); $client->setClientId($clientId); $client->setClientSecret($clientSecret); $client->setRedirectUri($redirectUri); $client->addScope("https://www.googleapis.com/auth/userinfo.profile"); $client->addScope("https://www.googleapis.com/auth/userinfo.email"); return $client; }
认证控制器(变量与构造函数)
private $clientId = '[variable]'; private $clientSecret = '[variable]'; private $redirectUri = 'https://localhost/source_web/ctm/login'; private $client; private $objOAuthService; private $userData = array(); public function __construct() { parent::__construct(); // $this->load->helper('google'); $this->Model = $this->M_myweb->set_table('customer'); // Load google auth config $this->client = loadGoogleLoginInfo($this->clientId, $this->clientSecret, $this->redirectUri); // Send Client Request $this->objOAuthService = new Google_Service_Oauth2($this->client); }
登录函数
public function login() { // Add Access Token to Session if (isset($_GET['code'])) { $this->client->authenticate($_GET['code']); $_SESSION['access_token'] = $this->client->getAccessToken(); header('Location: ' . filter_var($this->redirectUri, FILTER_SANITIZE_URL)); } // Set Access Token to make Request if (isset($_SESSION['access_token']) && $_SESSION['access_token']) { $this->client->setAccessToken($_SESSION['access_token']); } // Get User Data from Google and store them in $data if ($this->client->getAccessToken()) { $this->userData = $this->objOAuthService->userinfo->get(); } else { $authUrl = $this->client->createAuthUrl(); $this->data['authUrl'] = $authUrl; } if (!isEmpty($this->userData)) { $flag = $this->Model->set('oauthUid', $this->userData['id'])->get(); $res = $this->register($flag); $user = array( 'uId' => $flag->id, 'firstName' => $this->userData['given_name'], 'lastName' => $this->userData['family_name'], 'avatar' => $this->userData['picture'], 'is_admin' => false, 'logged_in' => true, 'token' => $_SESSION['access_token']['access_token'] ); if (!$res) { $user['role'] = 0; } else { $user['role'] = $flag->role; } $_SESSION['system'] = !isEmpty($user) ? (object) $user : null; } $this->data['userData'] = $this->userData; $this->data['title'] = 'Login'; $this->data['subview'] = 'customer/auth/V_home'; $this->load->view('default/_main_page', $this->data); }
注册校验函数
private function register($flag) { if (!isEmpty($flag)) { $user = array( 'avatar' => $this->userData['picture'], 'firstName' => $this->userData['given_name'], 'lastName' => $this->userData['family_name'], 'updateAt' => date('Y-m-d H:i:s') ); $this->Model->sets($user)->setPrimary($this->userData['id'])->save(); return true; } else { $user = array( 'oauthUid' => $this->userData['id'], 'role' => 0, 'account' => $this->userData['email'], 'avatar' => $this->userData['picture'], 'firstName' => $this->userData['given_name'], 'lastName' => $this->userData['family_name'], 'createAt' => date('Y-m-d H:i:s') ); $this->Model->sets($user)->save(); return false; } }
补充信息
- 已尝试ChatGPT给出的建议,均无效;
- 应用在我的本地机器上运行正常;
- 问题仅出现在队友的机器上;
- Google API控制台中的重定向URI配置正确。
期望排查方向
环境层面
- 队友本地服务的HTTPS配置:确认是否使用HTTPS启动服务,是否存在本地SSL证书不被信任的问题;
- 本地hosts文件映射:检查队友机器的hosts文件是否有异常配置,导致localhost解析错误;
- PHP版本与扩展差异:对比本地与队友的PHP版本,检查是否存在Google Client库依赖的PHP扩展缺失或版本不兼容;
- Session存储配置:排查队友机器的PHP Session配置(如
session.save_path),确认Session是否能正常读写; - 防火墙/代理拦截:检查队友机器是否有防火墙、代理软件拦截了OAuth回调请求,或修改了请求头信息。
配置层面
- 重定向URI动态生成:将硬编码的
$redirectUri改为动态获取(比如使用CodeIgniter的base_url()拼接路径),避免环境差异导致的URI不匹配; - Google Client库版本统一:确认队友机器安装的
google/apiclient库版本与本地完全一致; - CodeIgniter核心配置对比:检查
config/config.php中的base_url、cookie_domain、sess_save_path等与URL、会话相关的配置,确保和本地一致; - 添加调试日志:在登录函数中增加日志记录,输出
$_GET['code']接收情况、$client->authenticate()执行结果、Session存储状态,定位卡顿具体环节。
内容的提问来源于stack exchange,提问作者PTPZ Center
相关产品推荐
相关产品推荐

