2012-08-09 16 views
8

我有一個現有的遠程站點,其中有身份驗證訪問,現在我想結合本站在我自己的網站使用iframe。加載iframe時是否有任何解決方案可以幫助自動登錄遠程站點?在iframe中自動登錄遠程站點內

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

如果想訪問http://remote.com/list,登錄需要和POST用戶名/密碼的作品。如何在iframe加載時自動登錄?

這裏有一些限制

  • 登錄只能用POST方法的工作原理
  • 的iframe/JavaScript有跨域問題
  • 沒有登錄API提供
  • 沒有其他的修飾可以在遠程站點做

回答

0

無法完成。就那麼簡單。跨域限制是非常具體的,以阻止你能夠做到這樣的事情。

2

如果您擁有其他網站,那麼您可以嘗試通過某個令牌進行身份驗證。

將授權令牌傳遞給iframe中的網址。

+0

沒有令牌由遠程站點提供。 – linbo 2012-08-09 11:09:18

14

一切皆有可能。但是,由於披露了訪問遠程頁面的詳細信息,下面的解決方案非常不安全。

<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輸入的值是在客戶端讀取。

+0

我們可以在服務器端使用它來隱藏證書。 – 2017-01-30 09:00:22

+0

似乎是問題的最佳答案;我建議你接受這個 – lwohlhart 2017-08-17 16:09:51

2

這是我的實現這樣做。儘管如此,這並不能解決跨域問題。對於跨域問題,您可以嘗試使用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> 
+0

這真的很安全嗎?將用戶名和密碼硬編碼到客戶端可以下載的js文件中? – codea 2016-06-28 07:59:45

+0

這是我以* .hta加載身份驗證的