在 iframe 中自动登录远程站点

新手上路,请多包涵

我有一个可以访问身份验证的现有远程站点,现在我想使用 iframe 将此站点合并到我自己的站点中。有什么解决方案可以帮助加载 iframe 时自动登录远程站点吗?

 <iframe src="http://remote.com/list"></iframe>

如果要访问 http://remote.com/list ,需要登录并仅发布用户名/密码。加载iframe时如何自动登录?

这里有一些限制

  • 登录仅适用于 post 方法
  • iframe / javascript 存在跨域问题
  • 没有提供登录API
  • 无法在远程站点进行其他修改

原文由 linbo 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 590
1 个回答

一切皆有可能。但是,由于泄露了对远程页面的访问详细信息,下面的解决方案非常不安全。

 <form id="login" target="frame" method="post" action="http://remote.com/login">
    <input type="hidden" name="username" value="login" />
    <input type="hidden" name="password" value="pass" />
</form>

<iframe id="frame" name="frame"></iframe>

<script type="text/javascript">
    // submit the form into iframe for login into remote site
    document.getElementById('login').submit();

    // once you're logged in, change the source url (if needed)
    var iframe = document.getElementById('frame');
    iframe.onload = function() {
        if (iframe.src != "http://remote.com/list") {
            iframe.src = "http://remote.com/list";
        }
    }
</script>

usernamepassword 输入的值在客户端是可读的。

原文由 Jakub Hubner 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题