简体   繁体   English

如何在Play Framework中存储cookie?

[英]How do I store a cookie in Play Framework?

I want to store an authentication token with Play Framework that outlives the current session, perhaps for days or even weeks - so that users don't have to login every time. 我想在Play Framework中存储一个比当前会话更长的身份验证令牌,可能持续数天甚至数周 - 这样用户就不必每次都登录。

What is the recommended way to do this? 建议的方法是什么?

The response object has a method setCookie, which does exactly what you want 响应对象有一个方法setCookie,它完全符合您的要求

response.setCookie("playlonglivecookie", yourData, "14d");

Remember, that the data stored in the cookie is not encrypted, so if you want to encrypt it, then use the Crypto.sign method. 请记住,存储在cookie中的数据未加密,因此如果要对其进行加密,请使用Crypto.sign方法。 Which signs your code using the play framework secret key. 使用play框架密钥对您的代码进行签名。

http://www.playframework.org/documentation/api/1.1.1/play/mvc/Http.Response.html#setCookie(java.lang.String,%20java.lang.String) http://www.playframework.org/documentation/api/1.1.1/play/mvc/Http.Response.html#setCookie(java.lang.String,%20java.lang.String)

I would also advise you to have a look at the secure module provided in play-1.x/modules/secure and the file Secure.java... it provides a checkbox "remember me" in the login form which allows keeping you logged for eternity. 我还建议你看一下play-1.x / modules / secure和Secure.java文件中提供的安全模块......它在登录表单中提供了一个“记住我”的复选框,可以让你记录下来永恒。

and the code of this function (specially the response.setCookie at the end): 和这个函数的代码(特别是最后的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();
}

Pascal 帕斯卡尔

With play > 2.5 setCookie is deprecated. 随着游戏> 2.5,不推荐使用setCookie

you can use instead: 你可以改用:

Http.Response.setCookie(Http.Cookie cookie)

You can create a new cookie with the builder: 您可以使用构建器创建新cookie:

Http.Cookie.builder("name", "value").withMaxAge(15).build();

15 days is the expiration date 15天是到期日

Reference: https://www.playframework.com/documentation/2.5.x/api/java/play/mvc/Http.Response.html#setCookie-play.mvc.Http.Cookie- 参考: https//www.playframework.com/documentation/2.5.x/api/java/play/mvc/Http.Response.html#setCookie-play.mvc.Http.Cookie-

Example: https://github.com/playframework/playframework/blob/master/framework/src/play/src/test/java/play/mvc/CookieBuilderTest.java 示例: https//github.com/playframework/playframework/blob/master/framework/src/play/src/test/java/play/mvc/CookieBuilderTest.java

The response-object has some methods to help you. response-object有一些方法可以帮助你。 See javadoc . javadoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM