java - not able to read browser cookie using HttpClient -
i need check browser cookie (along id - password) decide if user valid or not. have set cookie in browser when user activates account. @ time of login need check cookie along credentials. using spring security (daoauthenticationprovider) authentication. setting cookie using code :
public void setcookie(string token, httpservletrequest request, httpservletresponse response) { try { cookie cookie = new cookie(cookiename, token); cookie.setdomain(cookiedomain); cookie.setpath(cookiepath); cookie.setmaxage(31536000); // seconds 365 days response.addcookie(cookie); } catch(exception e) { log.error("exception in setting cookie : {}", e.getmessage()); } }
when it's time read cookie, don't have httpservletrequest
object me. because it's not spring controller can have request object. wrote method in spring controller return cookie value. code :
@requestmapping(value = "gettoken", method = requestmethod.get, produces={"text/html"}) public responseentity<string> gettoken(httpservletrequest request) { string token = ""; cookie cookies[] = request.getcookies(); if (cookies != null) { (int = 0; < cookies.length; i++) { system.out.println("cookie details : " + cookies[i].getname() + "\t" + cookies[i].getvalue()); if (cookiename.equalsignorecase(cookies[i].getname())) { token = cookies[i].getvalue(); break; } } } else { logger.info("no cookies found..."); } return new responseentity<string>(token,httpstatus.ok); }
this spring controller action returns cookie value. when hit url, can see returned cookie value in browser.
now call url within application using httpclient. code :
public boolean iscookievalid(string token) { boolean valid = false; try { string requesturl = serverurl + "gettoken"; log.info("requesturl : {} ", requesturl); defaulthttpclient httpclient = new defaulthttpclient(); httpcontext localcontext = new basichttpcontext(); httpget getrequest = new httpget(requesturl.trim()); getrequest.addheader("accept", "text/html"); httpresponse response = httpclient.execute(getrequest,localcontext); system.out.println("call success : " + response.getstatusline().getstatuscode()); if (response.getstatusline().getstatuscode() != 200) { return false; } httpentity entity = response.getentity(); if (entity != null) { string cookietoken = entityutils.tostring(entity); system.out.println("cookie token : " + cookietoken); if(token.equalsignorecase(cookietoken)) { valid = true; } } httpclient.getconnectionmanager().shutdown(); } catch (exception e) { log.info("exception in reading cookies : {}", e.getmessage()); } return valid; }
when hit spring action using http client, receive http 200 response code. spring controller not able read cookie. idea problem? has better approach ?
gettoken()
extracts cookie request receives. so, how expect token it, when send request httpclient
without adding cookies request? when use browser call gettoken()
, browser sends cookies set earlier same webapp along request. that's why token back.
Comments
Post a Comment