lin*_*nbo 14 html javascript iframe autologin
我有一个现有的远程站点,其中包含要访问的身份验证,现在我想使用iframe在我自己的站点中组合此站点.加载iframe时是否有任何解决方案可以帮助自动登录远程站点?
<iframe src="http://remote.com/list"></iframe>
Run Code Online (Sandbox Code Playgroud)
如果想访问http://remote.com/list,请登录并仅发布用户名/密码.iframe加载时如何自动登录?
这是一些限制
Jak*_*ner 21
一切皆有可能.然而,由于向远程页面公开了访问细节,下面的解决方案非常不安全.
<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>
Run Code Online (Sandbox Code Playgroud)
客户端的值username和password输入都是可读的.
做不到。就那么简单。跨域限制是专门为了阻止您执行类似的操作而设置的。
唯一的例外是,如果您是远程站点的所有者并完全控制服务器,那么您可以添加 CORS 标头以允许您自己的外部服务访问该服务器的权限。
这是我执行此操作的实现。但是,这不能解决跨域问题。对于跨域问题,您可以尝试使用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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
52706 次 |
| 最近记录: |