【问题标题】:Passing authentication to a different domain Iframe将身份验证传递给不同的域 iframe
【发布时间】:2019-10-15 09:25:48
【问题描述】:

我必须使用需要完全相同身份验证的不同应用程序,并且我正在尝试将我的身份验证信息从 localStorage 从一个应用程序传递到另一个应用程序,而无需用户登录两次。我正在尝试使用以下指南,但我不确定如何继续:

https://levelup.gitconnected.com/share-localstorage-sessionstorage-between-different-domains-eb07581e9384

这是我想传递给我的 iframe 的信息:

token: 1234567890abcdef
user: someUserName
expires: 987654321

我的组件如下所示:

<template>
    <v-container grid-list-md text-xs-center>
      <v-content>
    <div>
      <ToolBar/>
    </div>
      <vue-friendly-iframe id="frame" name="frame" src="http://localhost:8081"></vue-friendly-iframe>
    </v-container>

</template>

<script>

import ToolBar from '@/components/component/ToolBar'

  const domains = [
    "http://localhost:8080",
    "http://localhost:8081"
  ]

  export default {

    name: 'Dashboard',

      components: {
        ToolBar
    },

    data () {
      return {
        //
      }
    },

  }
</script>

不确定如何在此处继续

【问题讨论】:

  • 你控制这两个应用程序吗?如果是这样,最简单的方法是使用代理(nginx 等)通过主域下的路由额外为框架应用程序提供服务。我在我的应用程序中正是出于同样的目的这样做,它使一切变得容易得多。
  • 是的,我愿意。我从来没有使用过 nginx,而且我一般是 veu 的新手,它们都运行在本地主机上,分别在端口 8080 和 8081 上。我将如何处理这个代码?

标签: vue.js iframe local-storage axios


【解决方案1】:

TLDR:这确实没有完美的解决方案。

话虽如此,您可以采取几种方法。

使用反向代理

正如@David 建议的那样,使用 Nginx 或 HAPorxy 之类的反向代理来为来自同一域的两个应用程序提供服务 - protocol://host:port。这三件事应该是相等的。

使用 cookie 代替 LocalStorage

如果您使用 cookie 而不是 LocalStorage,则主机端口不会参与确定站点策略。因此,在同一主机上运行但不同端口的两个应用程序将共享 cookie,而无需任何额外工作。要保护 cookie,请使用仅限 HTTP 的 cookie,即同站点 cookie。

使用 URL 共享 - 仅限 IFrame

如果您使用的是 iFrame,那么您可以使用 URL 来共享令牌。当外部窗口加载 iFrame 时,通过像 http://localhost:8081/somepage#token=1234 这样的哈希发送此信息

使用散列将允许页面将数据发送到内部页面,而无需通过网络发送。

使用 window.postMessage - 仅限 IFrame

使用window.postMessage,您可以简单地将所需数据传递给内部窗口/iFrame。只要控制两个端点,就可以轻松实现跨域消息发送。

最后,这真的取决于您的安全要求、易于维护等。

【讨论】:

  • 我在 iframe 中使用了 postMessage 的最后一种方法。我能够通过身份验证通过发布消息登录到另一个页面页面,但是它实际上并没有保存我的本地存储信息,但到目前为止这有所帮助。谢谢芽。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-12
  • 2019-11-18
  • 2015-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多