CORS requests with session/cookie

主宰稳场 提交于 2019-12-29 07:51:51

问题


My application has a PHP server and a client (a JS single-page app). They are separate projects and deployed in different domains. The client consumes a RESTful API exposed by the server.

This application is to be integrated with a third party which handles authentication, so users cannot login directly. Our server just receives an SSO token (which comes appropriately signed so that we verify its integrity).

We also enforce security at the transport layer for all requests.

What I'd like to do is, once the SSO token is verified, start a session of my own and then redirect the user to the client. I thought that once the session was created the browser would automatically send the right Cookie header in the asynchronous API calls, but it doesn't seem to be the case.

Is this deliberately disabled due to security reasons?


回答1:


You must set withCredentials to true for cross-origin XHR requests to include cookies.

The CORS response must also say Access-Control-Allow-Credentials: true (which is why widthCredentials defaults to false).




回答2:


I thought that once the session was created the browser would automatically send the right Cookie header in the asynchronous API calls

Not for cross-domain requests for CORS-enabled ressources (which seems to be the case here, if I understand your described setup correctly.)

To make that happen, you need to set the withCredentials flag.



来源:https://stackoverflow.com/questions/34968258/cors-requests-with-session-cookie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!