2013-03-06 64 views
3

從JavaScript我用:爲什麼我的服務器忽略來自ajax請求的驗證標頭?

xhr.setRequestHeader("Authorization", make_base_auth(username,password)); 

然而,HTTP請求不具有Authorization頭:

OPTIONS /restService/index?_=1362589672203 HTTP/1.1 
Host: myappinheroku.herokuapp.com 
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:19.0) Gecko/20100101 Firefox/19.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: es-MX,es-ES;q=0.8,es-AR;q=0.7,es;q=0.5,en-US;q=0.3,en;q=0.2 
Accept-Encoding: gzip, deflate 
Origin: http://127.0.0.1:8081 
Access-Control-Request-Method: GET 
Access-Control-Request-Headers: authorization,content-type 
Connection: keep-alive 

似乎驗證被完全忽略。哪裏不對?我們如何啓用CORS認證?


這是上述請求的服務器的響應:

HTTP/1.1 401 Full authentication is required to access this resource 
Access-Control-Allow-Credentials: true 
Access-Control-Allow-Headers: origin, authorization, accept, content-type, x-requested- with 
Access-Control-Allow-Methods: GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS 
Access-Control-Allow-Origin: * 
Access-Control-Max-Age: 3600 
Server: Jetty(7.x.y-SNAPSHOT) 
Set-Cookie: JSESSIONID=6smxjnlqelmc1lg98ain16wv7;Path=/ 
WWW-Authenticate: Basic realm="Ralph's Bait and Tackle" 
Transfer-Encoding: chunked 
Connection: keep-alive 
+0

你是如何獲得請求和回覆的?嘗試看看Fiddler並將它們與有效的請求進行比較。 – CodeCaster 2013-03-06 17:31:10

+1

@CodeCaster我得到與Firebug的請求,一個有效的請求發送這樣的事情: 授權:基本YXNkZjphZg ==但我的請求發送一個全局頭:訪問控制請求頭:授權,內容類型 – 2013-03-06 17:37:59

回答

7

*不能用於Access-Control-Allow-Origin頭時Access-Control-Allow-Credentialstrue。您需要將Access-Control-Allow-Origin設置爲原點本身的值(即在此情況下爲http://127.0.0.1:8081)。

另請注意,auth憑證不會在預檢請求上發送。他們只是在實際的請求發送。預檢僅用於驗證CORS請求是否被允許,它不應該自己進行任何驗證。

相關問題