youtube如何登录到Gmail帐户而不重定向?

cha*_*118 9 login single-sign-on google-login

第1步:我登录了我的Gmail帐户.浏览器实际上重定向到accounts.google.com.所以我登录并重定向回到登录的Gmail

第2步:现在在浏览器中输入youtube.com.没有任何重定向,我使用gmail帐户登录youtube.

Youtube是一个完全不同的领域.accounts.google.com没有任何重定向它如何通信?我通过Chrome开发者工具检查了网络请求,但看不到这样的重定向!

ped*_*ofb 13

这是允许使用中央sso域(accounts.google.com)在youtube或gmail等两个或更多网站之间进行跨域通信的技术解决方案

1)登录gmail重定向到accounts.google.com,识别您并使用您的帐户信息发出身份验证令牌(JWT格式).令牌存储在浏览器localStorage中

//Store the Json Web token with accountInfo in localStorage
localStorage.setItem('tokenId',jwt);
Run Code Online (Sandbox Code Playgroud)

2)Youtube检查accounts.google.com localStorage查找身份验证令牌.如果找到,允许您输入.

可以使用中间域在域之间共享Cookie和localStorage accounts.google.com.在主页上嵌入了一个iframe,它访问cookie并向主节点发送消息.

//Create iframe when page is loaded pointing to sso domain. For example in gmail.com and youtube.com pointing to accounts.google.com
var iframe = document.createElement('iframe');
iframe.style.display = "none";
iframe.src = 'https://sso.domain.com/sso.html?sourceDomain=...;
iframe.id = 'sso.iframe';
document.body.appendChild(iframe);
Run Code Online (Sandbox Code Playgroud)

加载iframe时,将带有jwt的消息发送到父页面

window.parent.postMessage(jwt, sourceDomain);
Run Code Online (Sandbox Code Playgroud)

父页面接收令牌

//Message listener for SSO events (created by the sso.iframe)
addEventListener("message", _listener, false);

function _listener(event){
    //origin check
    if (  sourceDomain.lastIndexOf(event.origin ) == -1){
        return;
    }

    var jwt = event.data
    //do something with the token...
 }
Run Code Online (Sandbox Code Playgroud)

因此domain1.com和domain2.com可以通过这种方式共享cookie/localStorage.打开Chrome-> Inspect-> Resources-> Local storage,您将在accounts.google.com中看到共享信息(有许多数据字段).

JWT是自包含的,并使用服务器密钥签名.它包含用户数据,并且可以验证发行者的完整性和身份

查看https://github.com/Aralink/ssojwt以这种方式查看SSO的实现,并解决与不同浏览器的所有问题

这是谷歌使用的一般模式.如果您浏览gmail或youtube代码,您将看到许多内容和其他附加字段.谷歌还添加了原产地限制.如果您想使用accounts.google.comSSO,您必须在谷歌应用程序中注册,获取集成ID并指定您的授权来源