我正在尝试通过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-用户名%3Akantaki
  • 2.)Security.Authenticator类做什么?
  • 3.)我认为通过cookie进行用户管理是一个非常普遍的问题,play!2.0为我提供了完整的解决方案吗?还是至少有一些文档?
  • 最佳答案

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

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

  • 通过电子邮件,Google,Facebook,Foursquare,Twitter,OpenId和自定义提供程序为registerlog in用户提供了可能。
  • 多语言支持(当前:英语,德语,波兰语)
  • 可自定义的模板(也用于引用电子邮件)
  • 支持rolespermissions(通过Deadbolt 2)
  • 密码恢复支持

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

    09-11 20:31
    查看更多