Class HttpSession
State kept for one client across requests, found again through a cookie.
@PostMapping("/login")
public void login(HttpServer.Request request, @RequestBody Map body) {
...
HttpSession session = request.getSession(true);
session.changeSessionId(); // never keep a pre-login id
session.setAttribute("user", userId);
}
A session is created only when something asks for one with
getSession(true), so a server that never does sets no cookie and keeps
nothing. The cookie is HttpOnly, SameSite=Lax and, on a TLS
server, Secure; its name, lifetime and store are configured under
cn1.session.*.
Attributes live in the SessionStore. The in-memory store keeps any
object; the database store keeps what Json can write -- strings,
numbers, booleans, and maps and lists of those -- because it has to read them
back in another process.
A session-scoped bean is never written to a store: the server that built it keeps it in memory for as long as the session lives, and runs its destroy methods when the session is invalidated, expires or the server stops. Another instance behind a load balancer builds its own.
-
Method Summary
Modifier and TypeMethodDescriptionGives the session a new id, keeping its attributes, and sends the client the new cookie.consumeAttribute(String name) Removes and returns an attribute once across every request and server sharing the session store.getAttribute(String name) <T> TgetAttribute(String name, Class<T> type) The attribute as atype, whichever store holds the session.The attribute names, as a copy.longgetId()The id the cookie carries.longintSeconds of inactivity after which the session is discarded.voidEnds the session: its attributes are dropped and the client's cookie cleared.booleanisNew()Whether this session was created by the current request.booleanisValid()voidremoveAttribute(String name) voidsetAttribute(String name, Object value) voidsetMaxInactiveInterval(int seconds)
-
Method Details
-
getId
The id the cookie carries. Secret: never log it. -
isNew
public boolean isNew()Whether this session was created by the current request. -
getCreationTime
public long getCreationTime() -
getLastAccessedTime
public long getLastAccessedTime() -
getMaxInactiveInterval
public int getMaxInactiveInterval()Seconds of inactivity after which the session is discarded. -
setMaxInactiveInterval
public void setMaxInactiveInterval(int seconds) -
getAttribute
-
getAttribute
The attribute as a
type, whichever store holds the session.The memory store hands back the object that was set; the database and key/value stores keep attributes as JSON and hand back its tree (a
Mapfor a record or other class). This reads either intotype, so code that switches stores keeps working. The build writes the JSON codec for a class passed here literally, and for the type of a value given tosetAttribute(String, Object)(issue 6353).- Returns:
- the attribute, or null when there is none
- Throws:
IllegalArgumentException- when it does not fittype
-
setAttribute
-
removeAttribute
-
consumeAttribute
Removes and returns an attribute once across every request and server sharing the session store. A stale request copy cannot consume it again.- Throws:
IOException- if the store cannot perform the atomic operation
-
getAttributeNames
The attribute names, as a copy. -
invalidate
public void invalidate()Ends the session: its attributes are dropped and the client's cookie cleared. -
isValid
public boolean isValid() -
changeSessionId
Gives the session a new id, keeping its attributes, and sends the client the new cookie. Call it when a user signs in: an id a client held before authenticating is one an attacker may have planted.
Returns
the new id
-