不要存储主要身份验证与Google登录
创始人
2025-01-10 18:00:29
0

如果您需要Google登录,最好不要存储主要身份验证。而是使用Google的ID令牌进行身份验证。以下是一个使用Google ID令牌进行身份验证的PHP示例代码:

1.首先获取用户的Google ID令牌:

$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->addScope(Google_Service_Oauth2::USERINFO_PROFILE);
$client->addScope(Google_Service_Oauth2::USERINFO_EMAIL);
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
$client->setRedirectUri($redirect_uri);
if (!isset($_GET['code'])) {
  $auth_url = $client->createAuthUrl();
  header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
  $client->authenticate($_GET['code']);
  $_SESSION['access_token'] = $client->getAccessToken();
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  exit;
}

2.使用此令牌进行Google OAuth身份验证:

$client = new Google_Client();
$client->setAuthConfig('client_secret.json');
$client->setAccessType('offline');
$client->setApprovalPrompt('auto');
$client->setAccessToken($_SESSION['access_token']);
$token_data = $client->verifyIdToken();
$userid = $token_data['sub'];

通过这种方式,您可以使用Google ID令牌进行身份验证,而无需将用户的主要凭据存储在您的数据库中。

相关内容

热门资讯

透视解迷!aapoker能控制... 透视解迷!aapoker能控制牌吗(透视)wepoker黑侠辅助器正版下载,教程要领(有挂方式)-哔...
透视科普!wepoker是不是... 透视科普!wepoker是不是有人用挂(透视)智星菠萝透视,教程绝活儿(有挂方略)-哔哩哔哩进入游戏...
透视详细!wepoker透视方... 透视详细!wepoker透视方法(透视)pokermaster破解版,教程指南书(有挂技巧)-哔哩哔...
透视了解!约局吧透视挂下载(透... 透视了解!约局吧透视挂下载(透视)hhpoker俱乐部是干嘛的,教程诀窍(真是有挂)-哔哩哔哩1、每...
透视解谜!pokemmo手机辅... 透视解谜!pokemmo手机辅助软件(透视)wpk俱乐部是做什么的,教程窍门(有挂教程)-哔哩哔哩1...
透视透视!wepoker私人局... 透视透视!wepoker私人局透视(透视)wepoker是不是有人用挂,教程技法(竟然有挂)-哔哩哔...
透视关于!拱趴大菠萝有挂吗(透... 透视关于!拱趴大菠萝有挂吗(透视)wepoker公共底牌,教程窍要(有挂方法)-哔哩哔哩1、拱趴大菠...
透视了解!wepoker能不能... 透视了解!wepoker能不能透视(透视)wepoker破解器有用吗,教程烘培(有挂秘笈)-哔哩哔哩...
透视总结!pokemmo辅助器... 透视总结!pokemmo辅助器脚本下载(透视)aapoker透视脚本,教程教程书(有挂总结)-哔哩哔...
透视开挂!hhpoker是内部... 透视开挂!hhpoker是内部控制吗(透视)we-poker靠谱吗,教程讲义(有挂方针)-哔哩哔哩1...