我正在尝试通过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));
}
现在我有几个问题/问题:
最佳答案
Joscha Feth还提供了authentication
和authorization
-Play Authenticate的完整堆栈。 (可在GitHub上获得)
它包含了Java的即用型示例,该示例使用了securesocial
+完整Deadbolt 2(由Steve Chaloner提供)支持的概念。它具有:
register
和log in
用户提供了可能。 roles
和permissions
(通过Deadbolt 2)其中有用于Java的示例应用程序。您可以将其合并到您的应用程序中。