Sticky Cookies in Scala

2019-09-03 16:23发布

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条回答
何必那么认真
2楼-- · 2019-09-03 16:55

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.

查看更多
登录 后发表回答