3

I am using angular and java building a small website.

I try to write a auth system, but i meet some problems with cookie.

My web app is run on localhost:8081 while my java servlet is on localhost:8888. In my java code:

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");

In my angular code:

$httpProvider.defaults.withCredentials = true; $httpProvider.defaults.useXDomain = true; delete $httpProvider.defaults.headers.common['X-Requested-With'];

actually I've tried reading Set-Cookie in HTTP header is ignored with AngularJS

Angularjs $http does not seem to understand “Set-Cookie” in the response

$http response Set-Cookie not accessible

but I still meet strange problems.

My first login: you can find that the server reponse a JSESSIONID and a mobile cookie in set-cookies header. first_login_picture

but I check the chrome developer tool and find the resource>cookies empty. console

after login, I send another request to the server to get some list: but the strange thing is chrome send the JSEESIONID which is send from sever to client on the first login but can't find in the chrome developer tool. while chrome don't send the 'mobile' cookie which is created by my self. after_login

I set my cookie in server with:

CookieUtils.setCookie(response, "mobile",String.valueOf(customer.getPhone()), 3600*24*7, "/","localhost");
request.getSession().setAttribute("user", customer);
request.getSession().setMaxInactiveInterval(3600*24);

what can i do if i want to get the cookie in $http response and set it, and when request other thing with this cookie like my 'mobile' cookie, because i want to do authentication this way.

while, the same code working with firefox is just ok, but not work in chrome 43.0.2357.65 m ,wtf..

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Zexi Li
  • 412
  • 1
  • 5
  • 11

1 Answers1

0

Adding

 $httpProvider.defaults.withCredentials = true;

with your requests, otherwise the cookie would be set.

Qingbao
  • 46
  • 7