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.
![enter image description here](https://i.stack.imgur.com/ltMrA.png)
现在用户已登录,他可以删除一些内容。因此前端向后端发送 GET 请求。因为我们在不同的域上工作,所以我们需要设置一些 CORS 标头,并且 Angular 在实际 GET 请求之前执行 OPTIONS 请求:
OPTIONS request:
![OPTIONS request](https://i.stack.imgur.com/GKMRf.png)
GET request
![GET request](https://i.stack.imgur.com/nRnSx.png)
我们通过 $http 模块在 Angular 中执行此操作,但它只是不会发送 cookie,其中包含JSESSIONID
.
如何让 Angular 发送 cookie?
在您的配置中,DI$httpProvider
然后将 withCredentials 设置为 true:
.config(function ($httpProvider) {
$httpProvider.defaults.withCredentials = true;
//rest of route code
})
有关 angularjs withCredentials 的信息:http://docs.angularjs.org/api/ng.$http
其中链接到 mozilla 文章:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)