§組態階段快取
Play 使用瀏覽器中的階段快取來儲存階段。在編寫程式時,您通常會透過 Scala API 或 Java API 來存取階段,但有一些有用的組態設定。
會話和快閃 Cookie 儲存在 JSON Web Token (JWT) 格式中。編碼對 Play 來說是透明的,但 JWT 有些有用的屬性可以用於會話 Cookie,而且可以透過 application.conf
加以設定。請注意,JWT 通常用於 HTTP 標頭值,這不是這裡的運作方式 - 此外,JWT 是使用密碼簽署,但 Play 沒有加密。
§Not Before 支援
當建立會話 Cookie 時,JWT 中的「已發布時間」iat
和「不早於」nbf
宣告會設定為 Cookie 建立時間,這可防止 Cookie 在目前時間之前被接受。
§會話逾時 / 到期
預設情況下,會話沒有技術逾時。它會在使用者關閉網路瀏覽器時到期。如果您需要特定應用程式的功能逾時,您可以透過在 application.conf
中設定金鑰 play.http.session.maxAge
來設定會話 Cookie 的最大存留時間,這也會將 play.http.session.jwt.expiresAfter
設定為相同的值。maxAge
屬性會從瀏覽器中移除 Cookie,而且 JWT exp
宣告會設定在 Cookie 中,並會在給定的時間後使它失效。
§URL 編碼 Cookie 編碼
會話 Cookie 使用 JWT Cookie 編碼。如果您想要,您可以透過在 application.conf 檔案中切換到 play.api.mvc.LegacyCookiesModule
來還原為 URL 編碼 Cookie 編碼
play.modules.disabled+="play.api.mvc.CookiesModule"
play.modules.enabled+="play.api.mvc.LegacyCookiesModule"
§會話設定
預設會話設定如下
# Session configuration
session = {
# The cookie name
cookieName = "PLAY_SESSION"
# Whether the secure attribute of the cookie should be set to true
secure = false
# The max age to set on the cookie.
# If null, the cookie expires when the user closes their browser.
# An important thing to note, this only sets when the browser will discard the cookie.
maxAge = null
# Whether the HTTP only attribute of the cookie should be set to true
httpOnly = true
# The value of the SameSite attribute of the cookie. Set to null for no SameSite attribute.
# Possible values are "lax", "strict" and "none". If misconfigured it's set to null.
sameSite = "lax"
# The domain to set on the session cookie
# If null, does not set a domain on the session cookie.
domain = null
# The session path
# Must start with /.
path = ${play.http.context}
jwt {
# The JWT signature algorithm to use on the session cookie
# uses 'alg' https://tools.ietf.org/html/rfc7515#section-4.1.1
signatureAlgorithm = "HS256"
# The time after which the session is automatically invalidated.
# Use 'exp' https://tools.ietf.org/html/rfc7519#section-4.1.4
expiresAfter = ${play.http.session.maxAge}
# The amount of clock skew to accept between servers when performing date checks
# If you have NTP or roughtime synchronizing between servers, you can enhance
# security by tightening this value.
clockSkew = 5 minutes
# The claim key under which all user data is stored in the JWT.
dataClaim = "data"
}
}
下一頁:設定 JDBC 連線池
在此文件發現錯誤?此頁面的原始程式碼可在 這裡 找到。閱讀 文件指南 後,請隨時提交拉取請求。有問題或建議要分享嗎?請前往 我們的社群論壇 與社群展開對話。