2013-02-26 141 views
6

基本上,我有一個登錄表單,一旦驗證它就啓動一個Oauth2進程。在完成獲取訪問令牌的最終重定向之前,它可以完美地工作。登錄流程如下:POST /user/login重定向到/user/oauth/auth然後重定向到Oauth redirect_uri /user/oauth/redirect。 redirect_uri永遠不會發生,並且請求永遠不會到達服務器。Safari Ajax cors請求不要重定向

如果我打的重定向使用Ajax請求Cors工作正常,我得到預期的響應似乎只發生如果我重定向兩次。

服務器顯示了這些響應

[I 130226 10:16:38 web:1462] 302 POST /user/login (192.168.1.5) 156.01ms (Session cookies set fine) 
[I 130226 10:16:38 web:1462] 200 OPTIONS /user/oauth/auth?scope=&client_id=DemoApp&response_type=code&redirect_uri=http://192.168.1.5:8443/user/oauth/redirect/code (192.168.1.5) 1.86ms 
302 GET /user/oauth/auth?scope=&client_id=DemoApp&response_type=code&redirect_uri=http://192.168.1.5:8443/user/oauth/redirect/code (192.168.1.5) 8.58ms 

最後瀏覽的網址永遠得不到的打擊。在Chrome,IE,FF流程是一樣的,除了服務器看到請求

[I 130226 10:16:38 web:1462] 302 POST /user/login (192.168.1.5) 156.01ms 
[I 130226 10:16:38 web:1462] 200 OPTIONS /user/oauth/auth?scope=&client_id=DemoApp&response_type=code&redirect_uri=http://192.168.1.5:8443/user/oauth/redirect/code (192.168.1.5) 1.86ms 
302 GET /user/oauth/auth?scope=&client_id=DemoApp&response_type=code&redirect_uri=http://192.168.1.5:8443/user/oauth/redirect/code (192.168.1.5) 8.58ms 
[I 130226 10:27:12 web:1462] 200 GET /user/oauth/redirect/code?code=57497058fbbf6003310ea22d3902ac67 (192.168.1.5) 0.54ms 

在Web檢查我看到了請求,但死像一個

enter image description here

我使用jQuery 1.9,這裏是Ajax請求(授予我得在這裏陽光下的每一個選項,試圖使它工作。)

$.ajax({ 
    type: "POST", 
    url: reqUrl, 
    data: data, 
    dataType: "json", 
    success: function(data, textStatus) { 
      console.log(data); 
      alert('logged in'); 

    }, 
    error: function(e){ 
     console.log(e); 
    }, 
    complete: function(request, status) { 
     console.log("headers=" + request.getAllResponseHeaders()); 
    }, 
    statusCode: { 
    200: function(data) { 
     console.log('yup we got it.') 
    } 
    }, 
    xhrFields: { 
    withCredentials: true 
    }, 
    crossDomain: true, 
    async:true 
}); 

JSONP不是一個選項作爲初始p ost通過https發送,並且必須是POST請求。包括IE在內的所有其他瀏覽器都運行得很好,它激發了成功的迴應。

Safari會觸發Statuscode 0和錯誤,這是經典的Origin策略錯誤,但是重定向uri在繞過登錄的標準ajax請求中工作。我懷疑這是一個訪問控制問題,因爲如果Safari只是解僱它,這個調用就可以工作。

我很確定它與重定向上的請求標題有關,Safari正在停止請求。在第一次成功的302,他們看起來像這樣

Access-Control-Request-Method: GET 
Origin: http://192.168.1.5:9090 
Access-Control-Request-Headers: origin, accept-encoding, accept-language 

但在重定向URI,就像這樣(這是有道理的,爲什麼它的失敗原因有沒有訪問控制標頭被髮送,但爲什麼?)

Origin: http://192.168.1.5:9090 
Accept-Encoding: gzip, deflate 
Accept-Language: en-us 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/536.26.17 (KHTML, like Gecko) Version/6.0.2 Safari/536.26.17 
Accept: */* 
Referer: http://192.168.1.5:9090/login 

回答

5

看起來這是一個Safari的東西,它只會遵循第一個重定向。 Apple聲稱這是HTML規範的編寫方式。所以......必須重做我的流程才能重定向/user/login並立即啓動oauth進程。 「

+1

」Apple聲稱這是HTML規範的編寫方式「 請引用您的來源嗎? – hrdwdmrbl 2015-10-19 18:24:53

+0

對不起,這真的很晚回覆。這是一位我正在與webkit交流的工程師。但它確實是HTML5規範。 設置請求原點爲空是規範的一部分http://www.w3.org/TR/cors/ 請參閱7.1.7節的步驟6。 – 2016-01-06 01:30:35

+0

我知道JSONP不適合你,但對於遇到此問題的其他人......我可以確認它可以在Safari上使用多個跨域重定向。 – james 2016-06-10 18:01:05