Play has no server session: the cookie is the session
I was building a small Play 3 / Scala lab today and asked where Play keeps the HTTP session. The answer: it doesn't. There is no session store, no in-memory map keyed by a session ID, nothing to replicate across nodes. The session is a cookie, and the cookie is a signed JWT that the browser carries back on every request.
I knew the word "stateless" from the Play docs. I had not really absorbed what it means until I decoded the cookie myself and then tried to forge it. This post is what I checked, in the order I checked it.
The lab: four actions, no storage
A controller with login, a "who am I" endpoint, a promotion that merges a key into the session, and logout. Nothing is persisted anywhere on the server.
class PlayStatelessController @Inject()(val controllerComponents: ControllerComponents)
extends BaseController {
def login(user: String): Action[AnyContent] = Action {
Redirect(routes.PlayStatelessController.me())
.withSession("user" -> user, "role" -> "reader") // replaces the whole session
.flashing("success" -> s"Welcome, $user")
}
def me(): Action[AnyContent] = Action { implicit request =>
val flash = request.flash.get("success").getOrElse("(no flash)")
request.session.get("user") match {
case Some(user) => Ok(s"flash: $flash, user: $user, raw: ${request.session.data}")
case None => Unauthorized(s"flash: $flash, no session")
}
}
def promote(): Action[AnyContent] = Action { implicit request =>
Redirect(routes.PlayStatelessController.me())
.addingToSession("role" -> "admin") // merges, so it needs the current request
.flashing("success" -> "Promoted to admin")
}
def logout(): Action[AnyContent] = Action {
Redirect(routes.PlayStatelessController.me()).withNewSession
}
}
Two API details matter. withSession replaces the entire session. addingToSession merges into it, which is why it needs the implicit request: it has to read the current session before writing a new one. And withNewSession deletes nothing on the server, because there is nothing there; it sends a Set-Cookie that expires the cookie.
What the browser receives
Calling the login route returns two cookies. Both values start with eyJhbGciOiJIUzI1NiJ9, which is base64url for {"alg":"HS256"}.
HTTP/1.1 200 OK
Set-Cookie: PLAY_SESSION=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7Im5hbWUiOiJ6YWthcmlhIn0sImV4cCI6...; Max-Age=3600; SameSite=Lax; Path=/; HTTPOnly
Set-Cookie: PLAY_FLASH=eyJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InN1Y2Nlc3MiOiJ3ZWxjb21lIGFsaWNlIn0s...; SameSite=Lax; Path=/; HTTPOnly

The Max-Age=3600 is there because I set play.http.session.maxAge = 1h. Without it the session cookie has no expiry and lives as long as the browser session.
Signed, not encrypted
A JWT is three base64url segments separated by dots: header, payload, signature. The payload needs no key to read:
$ cut -d. -f2 <<< "$PLAY_SESSION" | base64 -d
{"data":{"name":"zakaria"},"exp":1795242364,"nbf":1795238764,"iat":1795238764}

I also pasted the token and my dev secret into the jwt.io debugger and got "Signature Verified". The header and payload decoded without the secret; the secret only mattered for the green tick at the bottom.

So the guarantee is integrity, not confidentiality. The user can read their own session. They cannot change it without the server noticing.
Trying to forge it
This is the part I wanted to see with my own eyes. I took a genuine cookie, decoded the payload, swapped zakaria for mallory, re-encoded it and kept the original signature. Then I sent both versions to /lab/promote, which merges role: admin into whatever session it receives.
dec(){ local x=$(echo "$1" | tr '_-' '/+'); while [ $(( ${#x} % 4 )) -ne 0 ]; do x="$x="; done; echo "$x" | base64 -d; }
enc(){ printf '%s' "$1" | base64 -w0 | tr '+/' '-_' | tr -d '='; }
IFS=. read h p s <<< "$PLAY_SESSION"
forged=$(enc "$(dec "$p" | sed 's/zakaria/mallory/')")
curl -s -i -b "PLAY_SESSION=$h.$p.$s" http://localhost:9000/lab/promote # genuine
curl -s -i -b "PLAY_SESSION=$h.$forged.$s" http://localhost:9000/lab/promote # forged

With the genuine cookie, the new session contains name and role. With the forged one, only role. There was no exception in my code. A cookie with a bad signature simply becomes an empty session, and the merge had nothing to merge into. From the controller's point of view, the forger is just an anonymous user.
Flash: the same trick with a one-request lifetime
.flashing(...) writes a second signed cookie, PLAY_FLASH. On the next request the action reads it through request.flash, and Play clears it in that response:
Set-Cookie: PLAY_FLASH=; Max-Age=0; Expires=Thu, 01 Jan 1970 00:00:00 GMT; SameSite=Lax; Path=/

The bug I wrote while learning this
My first version always returned 401. Login wrote the key "name" and me() read "user". I also read the flash message with request.session.get("success") instead of request.flash. It compiled, it ran, and it was wrong in both places, because session and flash are just Map[String, String]. Put the keys in constants.
What this changes in practice
- Never put secrets or personal data in the session. Store an identifier and look up the sensitive data on the server.
- Keep it small. Browsers cap a cookie at about 4 KB, and it travels with every request.
- Logout is client-side. A copied cookie stays valid until it expires, so set
maxAge. Revoking one user needs server-side state again, such as a token version you check. - The secret is the whole security model. Keep it out of the repository, share it across every node, and know that rotating it logs everyone out at once. That is also your emergency switch.
- Horizontal scaling comes free. Any instance with the same secret can serve any request. No sticky sessions, no session cluster.
play.http.secret.key = ${?APPLICATION_SECRET} # long, random, shared by every node
play.http.session {
maxAge = 1h # adds "exp" to the JWT and Max-Age to the cookie
secure = true # HTTPS only in production
httpOnly = true # default: page JavaScript cannot read it
sameSite = "lax" # default
}
None of this is new: Play has signed its session and flash cookies as JWTs since 2.6. Seeing a forged payload quietly turn into an empty session made it concrete in a way the docs had not.