我有一个现有的远程站点,其中包含要访问的身份验证,现在我想使用iframe将此站点合并到我自己的站点中。加载iframe时是否有任何解决方案可以帮助自动登录远程站点?
<iframe src="http://remote.com/list"></iframe>
如果想要访问http://remote.com/list,请登录并仅发布用户名/密码。加载iframe时如何自动登录?
这是一些限制
答案 0 :(得分:20)
一切皆有可能。但是,由于向远程页面公开了访问详细信息,下面的解决方案非常不安全。
<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>
username
和password
输入的值在客户端可读。
答案 1 :(得分:4)
这是我执行此操作的实现。但这并不能解决跨域问题。对于跨域问题,您可以尝试使用jQuery的JSONP方法(我还没有尝试将jQuery与此解决方案结合使用)。
<iframe id="MyIFrame" width="400" height="400"></iframe>
<script type="text/javascript">
var iframeURL = 'http://mysite.com/path/applicationPage.aspx';
var iframeID = 'MyIFrame';
function loadIframe(){
//pre-authenticate
var req = new XMLHttpRequest();
req.open("POST",this.iframeURL, false, "username", "password"); //use POST to safely send combination
req.send(null); //here you can pass extra parameters through
//setiFrame's SRC attribute
var iFrameWin = document.getElementById(this.iframeID);
iFrameWin.src = this.iframeURL + "?extraParameters=true";
}
//onload, call loadIframe() function
loadIframe();
</script>
答案 2 :(得分:3)
如果您拥有其他网站,则可以尝试通过某种令牌进行身份验证。
将授权令牌传递给iframe中的url。
答案 3 :(得分:1)
无法完成。就那么简单。跨域限制非常具体,以阻止您做这样的事情。