在GAE应用程序中管理用户身份验证
问题描述:
在Google App Engine
应用程序中管理用户会话的最佳方式是什么?理想情况下,我希望保持我的应用程序无状态,并且不将任何用户相关数据保存在内存中,但是我也害怕在每个请求中发送网络用户凭据(更不用说在每个请求上验证用户都需要调用到花费钱的Datastore
)。在GAE应用程序中管理用户身份验证
我检出了google的OAuth 2.0
解决方案,但是从我的理解中可以看出,如果我的应用程序想要连接到任何google API并需要来自客户端的访问权限才能访问他的Google帐户,这会有所帮助。
有没有去管理用户会话的方式?最常见的情况是知道哪个用户发起了这个请求而不必发送userId
作为请求参数。
请注意,我们没有使用第三方供应商。用户通常在自己的页面注册自己,并拥有自定义帐户。我是而不是寻找有助于将验证与第三方服务集成的工具。否则,我会使用谷歌的OAuth 2.0
或类似的API
答
您可以始终实现身份验证器接口。
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。
Authomatic是一个很好的资源:https://github.com/authomatic/authomatic –
请检阅更新的问题。我**没有**寻找通过第三方提供商认证。 – Konstantine