我正在使用angular和java构建一个小型网站。
我尝试编写一个auth系统,但我遇到了一些cookie问题。
我的网络应用程序在localhost:8081上运行,而我的java servlet在localhost:8888上运行。 在我的java代码中:
response.addHeader("Access-Control-Allow-Origin", "http://localhost:8081");
response.addHeader("Access-Control-Allow-Headers", "X-Requested-With");
response.addHeader("Access-Control-Allow-Credentials", "true");
在我的角度代码中:
$httpProvider.defaults.withCredentials = true;
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
Angularjs $http does not seem to understand “Set-Cookie” in the response
$http response Set-Cookie not accessible
但我仍遇到奇怪的问题。
我的第一次登录:您可以发现服务器在set-cookies标头中响应JSESSIONID和移动cookie。
但我检查了chrome开发人员工具,发现资源> Cookie为空。
登录后,我向服务器发送另一个请求以获取一些列表:
但奇怪的是chrome发送了JSEESIONID,它在第一次登录时从服务器发送到客户端但在chrome开发工具中找不到。虽然chrome不会发送由我自己创建的“移动”cookie。
我在服务器中设置我的cookie:
CookieUtils.setCookie(response, "mobile",String.valueOf(customer.getPhone()), 3600*24*7, "/","localhost");
request.getSession().setAttribute("user", customer);
request.getSession().setMaxInactiveInterval(3600*24);
如果我想在$ http响应中获取cookie并设置它,并且在使用此cookie请求其他东西时,如果我的'移动'cookie,我该怎么办,因为我想以这种方式进行身份验证。
虽然,使用firefox的相同代码是正常的,但在chrome 43.0.2357.65 m,wtf ..
中无效。答案 0 :(得分:0)
添加
$httpProvider.defaults.withCredentials = true;
根据您的要求,否则将设置cookie。