Sticky Cookies in Scala

心不动则不痛 提交于 2019-12-25 03:42:42

问题


I've set a cookie in Scala similar to the following:

val cookies:Seq[Cookie] = new Seq()
val nDaysExpire:Int = 2000
val nSecondsExpire:Int = nDaysExpire * 24 * 60 * 60
val cookie:Cookie = new Cookie(sCookieID, sValue, Option(nSecondsExpire))
cookies = cookies:+ cookie
Ok(views.html.main(sID)).withCookies(cookies:_*)

and then I immediately delete the cookie in JavaScript. I have even deleted the cookie 30 seconds after the page has loaded.

When I reload the page, the Scala code still sees the cookie. But in the JavaScript, the cookie is no where to be found when I call: document.cookie.

What's going on?


回答1:


According to the docs, the constructor for Cookie takes a boolean parameter named httpCookie. The default value is true.

HttpOnly cookies cannot be seen by javascript. So, if you want to delete your cookie from javascript, try setting this to false.

val cookie:Cookie = new Cookie(sCookieID, sValue, Option(nSecondsExpire), httpOnly = false)

By Jeff Atwoord in Protecting Your Cookies: HttpOnly

When you tag a cookie with the HttpOnly flag, it tells the browser that this particular cookie should only be accessed by the server. Any attempt to access the cookie from client script is strictly forbidden.



来源:https://stackoverflow.com/questions/30581269/sticky-cookies-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!