2017-05-25 124 views
0

Google App Engine應用程序中管理用戶會話的最佳方式是什麼?理想情況下,我希望保持我的應用程序無狀態,並且不將任何用戶相關數據保存在內存中,但是我也害怕在每個請求中發送網絡用戶憑據(更不用說在每個請求上驗證用戶都需要調用到花費錢的Datastore)。在GAE應用程序中管理用戶身份驗證

我檢出了google的OAuth 2.0解決方案,但是從我的理解中可以看出,如果我的應用程序想要連接到任何google API並需要來自客戶端的訪問權限才能訪問他的Google帳戶,這會有所幫助。

有沒有去管理用戶會話的方式?最常見的情況是知道哪個用戶發起了這個請求而不必發送userId作爲請求參數。

請注意,我們沒有使用第三方供應商。用戶通常在自己的頁面註冊自己,並擁有自定義帳戶。我是而不是尋找有助於將驗證與第三方服務集成的工具。否則,我會使用谷歌的OAuth 2.0或類似的API

+0

Authomatic是一個很好的資源:https://github.com/authomatic/authomatic –

+0

請檢閱更新的問題。我**沒有**尋找通過第三方提供商認證。 – Konstantine

回答

0

您可以始終實現身份驗證器接口。

 public class MyAuthenticator implements Authenticator { 
    @Override 
    public User authenticate(HttpServletRequest request) { 
     HttpSession session = request.getSession(false); 
     // 
     return null;// if not authenticated, otherwise return User object. 
    } 
} 

// Endpoints class. 
@Api(name = "example", authenticators = { MyAuthenticator.class }) 
public class MyEndpoints { 
    public Profile getProfile(User user) { 

     if (user == null) { 
      throw new UnauthorizedException("Authorization required"); 
     } 
     return new Profile(user.getEmail(), "displayName"); 
    } 

    // store this class somewhere in models 
    public class Profile { 
     private String email; 
     private String displayName; 

     public Profile(String email, String displayName) { 
      this.email = email; 
      this.displayName = displayName; 
     } 

     public String getEmail() { 
      return email; 
     } 




     public String getdisplayName() { 
      return displayName; 
     } 
    } 
} 

使用HttpServletRequest對象來實現基於經典會話的登錄或使用您自己的自定義標頭。那取決於你的情況。未通過身份驗證時返回null,並在身份驗證時返回User對象。還要在雙方(客戶端和服務器)上實施某種加密,以阻止擁有會話密鑰的人訪問您的api。

相關問題