我想存儲一個身份驗證令牌與Play Framework超過當前會話,可能幾天甚至幾周 - 這樣用戶就不必每次都登錄。如何在Play Framework中存儲Cookie?
什麼是做到這一點的推薦的方法?
我想存儲一個身份驗證令牌與Play Framework超過當前會話,可能幾天甚至幾周 - 這樣用戶就不必每次都登錄。如何在Play Framework中存儲Cookie?
什麼是做到這一點的推薦的方法?
相應對象的方法setCookie方法,這不正是你想要的
response.setCookie("playlonglivecookie", yourData, "14d");
請記住,存儲在cookie中的數據沒有加密,因此如果要加密它,請使用Crypto.sign
方法。它使用播放框架密鑰爲您的代碼簽名。
如果您希望它可以在所有子域中共享,您將如何存儲cookie?我試圖做到這一點,但它不產生一個cookie:response.setCookie(「webLoggedIn」,「true」,「mydomain.org」,「」,2592000,false,false); – HelpMeStackOverflowMyOnlyHope 2016-04-26 03:16:39
響應對象有一些方法來幫助你。見javadoc。
我也建議你去看看在播放的1.x /模塊/安全和文件Secure.java提供的安全模塊...它的登錄表單提供了一個複選框「記住我」,這可以讓你保持永恆。
這個功能(特別是在response.setCookie末)的代碼:
public static void authenticate(@Required String username, String password, boolean remember) throws Throwable {
// Check tokens
Boolean allowed = false;
try {
// This is the deprecated method name
allowed = (Boolean)Security.invoke("authentify", username, password);
} catch (UnsupportedOperationException e) {
// This is the official method name
allowed = (Boolean)Security.invoke("authenticate", username, password);
}
if(validation.hasErrors() || !allowed) {
flash.keep("url");
flash.error("secure.error");
params.flash();
login();
}
// Mark user as connected
session.put("username", username);
// Remember if needed
if(remember) {
response.setCookie("rememberme", Crypto.sign(username) + "-" + username, "30d");
}
// Redirect to the original URL (or /)
redirectToOriginalURL();
}
帕斯卡爾
一起玩> 2.5 setCookie方法已被棄用。
,您可以改用:
Http.Response.setCookie(Http.Cookie cookie)
您可以創建一個新的cookie與建築商:
Http.Cookie.builder("name", "value").withMaxAge(15).build();
15天的截止日期
請檢查答案以關閉該問題。它也會幫助其他人。謝謝:) – 2011-05-09 16:20:05