Home > Enterprise >  How to specify the domain of cookie with Scala and Play
How to specify the domain of cookie with Scala and Play

Time:11-13

I want cookies which is set from test.domain.com to be set for .domain.com so that that it can still be used from anothertest.domain.com. Basically cookies should be shared between subdomains.

I called backend deployed at test.domain.com and set cookies with OK response as follows:


Ok("some response").withCookies(Cookie("id", id), Cookie("token", token))

And in application.conf I have set the session domain to ".domain.com"-


session {
\#Sets the cookie to be sent only over HTTPS.
\#secure = true

\#Sets the cookie to be accessed only by the server.
\#httpOnly = true

\#Sets the max-age field of the cookie to 5 minutes.
\#NOTE: this only sets when the browser will discard the cookie. Play will consider any
\#cookie value with a valid signature to be a valid session forever. To implement a server side session timeout,
\#you need to put a timestamp in the session and check it at regular intervals to possibly expire it.
\#maxAge = 300

\#Sets the domain on the session cookie.
domain = ".domain.com"
}

However, the cookie is being set for test.domain.com rather than .domain.com. I want to use this cookie with anothertest.domain.com .
Can you please help me with this.

CodePudding user response:

You don't have to change the configuration, you can add all attributes of a cookie when creating it.

Cookie("bla", bla).withDomain(xxx)
// Or
Cookie("bla", bla, domain = XXX)

(Not sure of exact name, I don't have documentation with me right now)

  • Related