$ HTTP不会发送请求的cookie

问题描述:

我们正在与AngularJS和Java Servlets一个RESTful Web服务。
当用户登录时,我们的后端发送一个设置Cookie头的前端。在角我们通过$饼干(ngCookie - 模块)访问头,并设置它。

We are working on a RESTful Webservice with AngularJS and Java Servlets. When the user logs in, our backend sends a "Set-Cookie" header to the frontend. In Angular we access the header via $cookies (ngCookie - module) and set it.

现在该用户登录,他可以例如删除一些东西。因此,前端发送一个GET请求到后端。因为我们在不同领域的工作,我们需要设置一些CORS页眉和角度进行实际的GET请求之前OPTIONS请求:

Now that the user is logged in he can for example delete some stuff. Therefore the frontend sends a GET request to the backend. Because we work on different domains we need to set some CORS Headers and Angular does an OPTIONS request before the actual GET request:

OPTIONS请求:

OPTIONS request:

GET请求

GET request

我们通过$ HTTP模块做到这一点的角,但它只是将不会发送的cookie,包含 JSESSIONID

We do this in Angular via $http module, but it just won't send the cookie, containing JSESSIONID.

我怎样才能使角发送的cookie?

在你的配置,DI $ httpProvider然后设置withCredentials为true:

In your config, DI $httpProvider and then set withCredentials to true:

.config(function ($httpProvider) {
    $httpProvider.defaults.withCredentials = true;
    //rest of route code
}

在angularjs withCredentials信息: http://docs.angularjs.org/api/ng。 $ HTTP

Info on angularjs withCredentials: http://docs.angularjs.org/api/ng.$http

可链接到Mozilla的文章:https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control#section_5

Which links to the mozilla article: https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS?redirectlocale=en-US&redirectslug=HTTP_access_control#section_5