0

Requirement : Our application needs to support same user opening our web application as separated session.

The problem is not how to use cookies in angular 2, but how can sever get cookie from HTTPServletRequest object when angular 2 application makes a rest call to server.

Implementation: Server side restful application has one filter to set user's browser session in cookie and then in HttpServletResponse. Angular client is making one call upon application bootstrap, which is going through server filter to set user's browser session in cookie.

Problem statement: angular client is making first rest call which goes through server filter to set the browser session cookie. When i open chrome developer tool, i do see that rest api response has "set-cookie" which has cookie set by server, but when i open the application tag in developer tool, i do not see any cookie set. After that if I make any other rest call through angular application, it does not send the cookie in either request or request headers. Now, our application rest api depends on this cookie value to be present in HttpServletRequest and now it is failing.

Can someone please guide me here? I must have done something wrong on angular 2 application side, which i am not able to catch. I have tried passing "withCredentials =true", but no change.

Another thing I noticed, if i make "GET" request, then i do see cookie in request header, but for "POST" request, I do not see anything for cookie.

Please advice.

server side code to set cookie

String uniqueId = RandomStringUtils.randomAlphanumeric(32); 
Cookie userSessionCookie = new Cookie("userSessionId", uniqueId); 
if (getDefaultDomain() != null) { 
    userSessionCookie.setDomain(getDefaultDomain()); 
} 
httpServletResponse.addCookie(userSessionCookie); httpServletResponse.addHeader("Access-Control-Allow-Credenti‌​als", "true"); httpServletResponse.addHeader("access-control-allow-methods"‌​, "GET, POST, PUT, PATCH, DELETE, OPTIONS"); 
httpServletResponse.addHeader("Access-Control-Allow-Headers"‌​, "Content-Type, token,withCredentials");

angular 2 post request which expects server to get cookie from HttpServletRequest

 renderFF() { 
         //prepare renderFInput object 
         var fcRenderInput = {}; 
         let headers = new Headers({ 'Content-Type': 'application/json' }); 
         let options = new RequestOptions({ headers: headers, withCredentials: true 
                }); 
         this._http.post('/api/v1/render/feature/fc',fcRenderI‌​nput,options) 
             .subscribe((res) => { 
                                  console.log(res.json()); 
          }); 
    } 
user2030613
  • 93
  • 1
  • 9
  • Possible duplicate of [Angular 2 cookies](http://stackoverflow.com/questions/34298133/angular-2-cookies) – Neil Lunn May 22 '17 at 02:38
  • My problem is related to this question on stackoverflow, but there also i do not see any solution. http://stackoverflow.com/questions/41747760/unable-to-send-cookies-through-angular2-web-app-in-post-request?rq=1 – user2030613 May 22 '17 at 05:07
  • You might make this easier for people to understand if you show the code you are trying. Then someone might be able to spot what is wrong. – Neil Lunn May 22 '17 at 05:13
  • Please use the [Edit](http://stackoverflow.com/posts/44103595/edit) link on your question instead of trying to post code in comments. – Neil Lunn May 22 '17 at 05:22
  • sorry for the bad formatting.. not sure how to format the code here – user2030613 May 22 '17 at 05:22
  • i suffer the same problem i see it. Its around one month ago if you solve this then kindly self answer your question.May be it helps me out from my problem. Thanks In advance. – Kiwi Rupela Jun 29 '17 at 12:42

1 Answers1

0

Just a suggestion if this is about only one browser and multiple tabs, in this case you can use the local storage while setting some flag in it. Also when you try to open the same application in the new tab. you check if the flag is there and user is trying to open the same web application in some other tab of the same browser. You also need to delete the local storage you had set after some point. I hope if you can get some trick to solve this issue :)

Deepender Sharma
  • 460
  • 1
  • 5
  • 25
  • Thanks Deepender for the suggestion. The idea is possible, but I do not want angular 2 application to send that flag for each and every request. If we set it in browser cookie, then it should automatically get it through request object. – user2030613 May 22 '17 at 05:41
  • I was talking about the check to be done only at client side. Do not load the app and show some popup message or alert saying you have already opened the app, if you have value in the local storage. I think it is possible without the server interfere – Deepender Sharma May 22 '17 at 06:02
  • But the requirement is to allow user to open multiple browser sessions with the web application and manage different sessions for the same user on server side – user2030613 May 22 '17 at 06:10