簡體   English   中英

Java Play! 2 - 使用cookie進行用戶管理

[英]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()方法),此批注允許將客戶端重定向到另一個頁面。

工作流程如下:

  1. 檢查授權
  2. 在客戶端cookie中添加唯一標識符
  3. 檢查是否經過驗證
  4. 保護控制器或操作
  5. 如果未獲得授權,請將客戶端重定向到另一個頁面

還有用於authenticationauthorization完整堆棧 - 由Joscha Feth 播放身份驗證 (可在GitHub獲得

它結合了Java的即用型樣本,它使用了securesocial +完整Deadbolt 2 (由Steve Chaloner)支持的概念。 它有:

  • 通過電子郵件,Google,Facebook,Foursquare,Twitter,OpenId和自定義提供商registerlog in用戶。
  • 多語言支持(目前:英語,德語,波蘭語)
  • 可自定義的模板(也用於信息電子郵件)
  • 支持rolespermissions (通過Deadbolt 2
  • 密碼恢復支持

其中有Java示例應用程序。 您可以將其合並到您的應用中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM