码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【我不是熟悉的javascript】使用postMessage+iframe实现授权登录


    需求

    从A网站,跳转到B网站,并且把A网站的登录token,带到B网站,使B网站处于登录状态

    【此方法 如果使用cookie作为登录状态,在A、B为非同站点(SameSite)的情况下不会生效,因为iframe 在非 同站点 的情况下无法携带cookie, 参考,参考】

    所以可以使用localstorage,localstorage在iframe中使用没有限制, 但是localstorage二级域名和根域名不能共享,除非手动在二级域名下设置document.domain = “根域名”!在iframe中更新localstorage会会被同步更新到网站中

    实现方法

    1. 在A网站,设置跳转按钮Button
    2. 在A网站,点击Button,创建一个display:none的iframe,将iframe的 src = B网站的授权登录页面authPage
    3. 在A网站,监听message事件
    4. 在B网站的authPage,监听message事件
    5. 在B网站的authPage,初始化 onload / mounted 之后,使用postMessage发送消息 isReady
    6. 在A网站监听的 message 事件,监听到isReady消息,使用postMessage发送消息setToken
    7. 在B网站的 authPage 监听的 message 事件,监听到setToken消息,使用该token进行登录等一系列操作
    8. B网站登录之后,使用postMessage发送消息close
    9. 在A网站中监听的message事件,监听到close消息,使用location.href跳转到B网站
    10. 此时A,B都使用同一个token,处于登录状态

    A网站的代码

    1. // A网站 Button按钮的方法
    2. function openB() {
    3. const iframeEle = document.createElement('iframe');
    4. iframeEle.src = `https://B.com/authPage?t=${Math.random(1, 100)}`;
    5. iframeEle.id = 'iframeEle';
    6. iframeEle.setAttribute('data-url','https://B.com/');
    7. iframeEle.style.display = 'none';
    8. document.body.appendChild(iframeEle);
    9. }
    10. // A 网站中 test.vue的mounted()方法 (或者其他的初始化方法)
    11. function mounted() {
    12. // 监听message方法
    13. window.addEventListener('message', receiveMessage);
    14. }
    15. function receiveMessage(e) {
    16. const iframeEle = document.getElementById('iframeEle');
    17. if (e.data?.action === 'isReady') {
    18. iframeEle.contentWindow.postMessage(
    19. {
    20. token: 'this is your token in A website',
    21. action: 'setToken'
    22. },'*');
    23. }
    24. if (e.data?.action === 'close') {
    25. const url = iframeEle.getAttribute('data-url');
    26. document.body.removeChild('iframeEle');
    27. // 接收到关闭消息时,跳转到B网站
    28. window.location.href = url;
    29. }
    30. }

    B 网站的代码

    1. // B 网站的mounted方法
    2. function mounted() {
    3. // window.parent 说明是在iframe中引入的
    4. if (window.parent) {
    5. window.parent.postMessage({ action: 'isReady' }, '*')
    6. window.addEventListener('message', receiveMessage, false)
    7. }
    8. }
    9. function receiveMessage(e) {
    10. if (e.data && e.data.action === 'setToken') {
    11. // 使用新token 登录
    12. loginByToken(e.data.token).finally(() => {
    13. // 注意,这里要在loginByToken里面的异步请求,都完成之后才发送close消息
    14. // 否则,如果loginByToken中的请求没返回,就发消息让A网站执行跳转操作
    15. // B网站就不会登录成功,因为未返回的请求被浏览器给取消了!!
    16. e.source.postMessage({ action: 'close' }, '*')
    17. })
    18. }
    19. }

    关于postMessage的相关知识请看官网

    Window.postMessage() - Web APIs | MDNThe window.postMessage() method safely enables cross-origin communication between Window objects; e.g., between a page and a pop-up that it spawned, or between a page and an iframe embedded within it.https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

    扩展知识

    1. 同源和同站的区别

    彻底搞懂「同源same-origin」和「同站same-site」的区别 - 掘金TLD 表示顶级域名,例如 .com、.org、.cn 等等,不过顶级域名并不是一成不变的,会随着时间推移增加,例如前段时间就增加了 .gay 顶级域名,可以说是广大同性交友爱好者的福音,目前所有顶级域名被收录到了顶级域名数据库里面。 eTLD 由 Mozilla 维护在公共后…https://juejin.cn/post/68774967815052001422. cookie 的same-site 限制

    Cookie 的 SameSite 属性 - 阮一峰的网络日志https://www.ruanyifeng.com/blog/2019/09/cookie-samesite.html

  • 相关阅读:
    使用wireshark分析tcp握手过程
    Three.js 材质的 blending
    【Linux】—— 详解动态库和静态库
    【微服务33】分布式事务Seata源码解析一:在IDEA中启动Seata Server
    牛客错题笔记
    hibernate及SpringBoot集成Jpa实现对数据库操作
    前端笔记:Create React App 初始化项目的几个关键文件解读
    图扑数字孪生卡车装配生产线,工业元宇宙还远么?
    LLMs之RAG:利用langchain实现RAG应用五大思路步骤—基于langchain使用LLMs(ChatGPT)构建一个问题回答文档的应用程序实战代码
    手机银行体验性测试:如何获取用户真实感受
  • 原文地址:https://blog.csdn.net/qq_17335549/article/details/127776863
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号