[英]Java Play! 2 - User management with cookies
我試圖通過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不是dectypted,看起來總是一樣。 例如bdb7f592f9d54837995f816498c0474031d44c1a-username%3Akantaki
2.)Security.Authenticator類有什么作用?
3.)我認為通過cookie進行用戶管理是一個非常普遍的問題,確實可以玩!2.0為我提供了完整的解決方案嗎? 或者至少有一些文件?
如Zentask示例所示,您的Secured
類應該擴展Security.Authenticator
。
有了這個,它將允許在Controller或Action上放置@Security.Authenticated
注釋。 如果未正確授權用戶(通過覆蓋Security.Authenticator.onUnauthorized()
方法),此批注允許將客戶端重定向到另一個頁面。
工作流程如下:
還有用於authentication
和authorization
完整堆棧 - 由Joscha Feth 播放身份驗證 。 (可在GitHub獲得 )
它結合了Java的即用型樣本,它使用了securesocial
+完整Deadbolt 2 (由Steve Chaloner)支持的概念。 它有:
register
和log in
用戶。 roles
和permissions
(通過Deadbolt 2 ) 其中有Java示例應用程序。 您可以將其合並到您的應用中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.