6

我正在尝试通过 cookie 管理我的用户。这并不容易,因为绝对没有关于这个主题的文档。

在示例“zentask”的帮助下,我做了这个:

session("username", filledForm.field("username").value());

public class Secured{

    public static Session getSession() {
        return Context.current().session();
    }

    public static String getUsername() {
        return getSession().get("username");
    }

    public static boolean isAuthorized() throws Exception {
        String username = getUsername();
        if (username == null)
            return false;
        long userCount = DatabaseConnect.getInstance().getDatastore()
                .createQuery(User.class).field("username").equal(username)
                .countAll();

        if (userCount == 1)
            return true;

        return false;

    }

我这样使用它:

public static Result blank() throws Exception {

        if (Secured.isAuthorized())
            return ok(Secured.getUsername());
        else
            return ok(views.html.login.form.render(loginForm));

    }

现在我有几个问题/问题:

  • 1.) Cookie 没有被删除,并且看起来总是一样的。例如 bdb7f592f9d54837995f816498c0474031d44c1a-username%3Akantaki

  • 2.) Security.Authenticator 类做什么?

  • 3.) 我认为通过 cookie 进行用户管理是一个很常见的问题,play!2.0 是否为我提供了完整的解决方案?或者至少有一些文件?

4

2 回答 2

12

Zentask 示例所示,您的Secured类应该扩展Security.Authenticator

有了这个,它将允许@Security.Authenticated在控制器或动作上放置注释。如果用户未正确授权(通过覆盖Security.Authenticator.onUnauthorized()方法),此注释允许将客户端重定向到另一个页面。

工作流程如下:

  1. 检查授权
  2. 在客户端 cookie 中添加唯一标识符
  3. 检查是否经过身份验证
  4. 保护控制器或操作
  5. 如果未授权,将客户端重定向到另一个页面
于 2012-08-05T12:50:35.787 回答
12

还有完整的堆栈authenticationauthorization- Joscha Feth 的Play Authenticate。(可在GitHub 上获得)

它包含了适用于 Java 的即用型securesocial示例,该示例使用+ 完整的Deadbolt 2(由 Steve Chaloner)支持的概念。它有:

  • 内置电子邮件、Google、Facebook、Foursquare、Twitter、OpenId 和自定义提供商的用户registerlog in
  • 多语言支持(目前:英语、德语、波兰语)
  • 可定制的模板(也适用于信息电子邮件)
  • 支持rolespermissions(通过Deadbolt 2
  • 密码恢复支持

其中有 Java 的示例应用程序。您可以将其合并到您的应用程序中。

于 2012-08-05T13:32:28.607 回答