如何在 GAE 端点中检索自定义用户对象?
How to retrieve custom User object in GAE endpoints?
我刚刚在我的 google 应用程序引擎 Java 应用程序上创建了我自己的自定义身份验证。这并没有像我接下来要做的那样麻烦。
身份验证工作正常,但现在我正在尝试向默认用户对象添加一些额外的字段,这样我就不必对服务器进行如此多的调用。
到目前为止,我所做的是创建一个实现 Authenticator 的自定义 class。基于用户是否经过身份验证,身份验证方法 returns User 对象或 null。然后我的 API 端点可以访问用户对象。
为了扩展我的应用程序功能,我尝试扩展默认用户对象,创建一些新字段,然后将其传递给端点。但是,由于端点可访问的用户对象与我从中扩展的用户对象不同,因此我无法获得额外的字段。
MyAuthenticator.java
import com.google.api.server.spi.auth.common.User;
public class MyAuthenticator implements Authenticator {
@Override
public User authenticate(HttpServletRequest request) {
// some code
return new AuthUser(...)
}
AuthUser.java
import com.google.api.server.spi.auth.common.User;
public class AuthUser extends User {
private String newToken;
public AuthUser(String email) {
super(email);
}
public AuthUser(String id, String email) {
super(id, email);
}
public AuthUser(String id, String email, String newToken) {
super(id, email);
this.newToken = newToken;
}
public String getNewToken() {
return newToken;
}
}
UserEndpoint.java
import com.google.appengine.api.users.User;
@Api(authenticators = MyAuthenticator.class)
public class UserEndpoint {
@ApiMethod(httpMethod = "GET")
public final Response sth(User user)
throws UnauthorizedException {
EndpointUtil.throwIfNotAuthenticated(user);
// ...
}
注意不同的 class 导入。
我不能在 UserEndpoint sth 方法中使用 AuthUser,因为 API 希望我在调用服务器时 post 该对象。
如何将额外数据从身份验证器传递到我的端点方法?
AppEngine docs 说注入的类型如下:
- com.google.appengine.api.users.User
- javax.servlet.http.HttpServletRequest
- javax.servlet.ServletContext
然而,它没有提到 com.google.api.server.spi.auth.common.User,但它确实有效。我刚刚尝试使用 AppEngine Java SDK 1.9.32。我不知道这是错误还是功能。
因此在 UserEndpoint.java 中,您必须导入 com.google.api.server.spi.auth.common.User,然后才能将其转换为 AuthUser。
import com.google.api.server.spi.auth.common.User;
@Api(authenticators = MyAuthenticator.class)
public class UserEndpoint {
@ApiMethod(httpMethod = "GET")
public final Response sth(User user)
throws UnauthorizedException {
EndpointUtil.throwIfNotAuthenticated(user);
((AuthUser)user).getNewToken();
// ...
}
我刚刚在我的 google 应用程序引擎 Java 应用程序上创建了我自己的自定义身份验证。这并没有像我接下来要做的那样麻烦。
身份验证工作正常,但现在我正在尝试向默认用户对象添加一些额外的字段,这样我就不必对服务器进行如此多的调用。
到目前为止,我所做的是创建一个实现 Authenticator 的自定义 class。基于用户是否经过身份验证,身份验证方法 returns User 对象或 null。然后我的 API 端点可以访问用户对象。
为了扩展我的应用程序功能,我尝试扩展默认用户对象,创建一些新字段,然后将其传递给端点。但是,由于端点可访问的用户对象与我从中扩展的用户对象不同,因此我无法获得额外的字段。
MyAuthenticator.java
import com.google.api.server.spi.auth.common.User;
public class MyAuthenticator implements Authenticator {
@Override
public User authenticate(HttpServletRequest request) {
// some code
return new AuthUser(...)
}
AuthUser.java
import com.google.api.server.spi.auth.common.User;
public class AuthUser extends User {
private String newToken;
public AuthUser(String email) {
super(email);
}
public AuthUser(String id, String email) {
super(id, email);
}
public AuthUser(String id, String email, String newToken) {
super(id, email);
this.newToken = newToken;
}
public String getNewToken() {
return newToken;
}
}
UserEndpoint.java
import com.google.appengine.api.users.User;
@Api(authenticators = MyAuthenticator.class)
public class UserEndpoint {
@ApiMethod(httpMethod = "GET")
public final Response sth(User user)
throws UnauthorizedException {
EndpointUtil.throwIfNotAuthenticated(user);
// ...
}
注意不同的 class 导入。
我不能在 UserEndpoint sth 方法中使用 AuthUser,因为 API 希望我在调用服务器时 post 该对象。
如何将额外数据从身份验证器传递到我的端点方法?
AppEngine docs 说注入的类型如下:
- com.google.appengine.api.users.User
- javax.servlet.http.HttpServletRequest
- javax.servlet.ServletContext
然而,它没有提到 com.google.api.server.spi.auth.common.User,但它确实有效。我刚刚尝试使用 AppEngine Java SDK 1.9.32。我不知道这是错误还是功能。
因此在 UserEndpoint.java 中,您必须导入 com.google.api.server.spi.auth.common.User,然后才能将其转换为 AuthUser。
import com.google.api.server.spi.auth.common.User;
@Api(authenticators = MyAuthenticator.class)
public class UserEndpoint {
@ApiMethod(httpMethod = "GET")
public final Response sth(User user)
throws UnauthorizedException {
EndpointUtil.throwIfNotAuthenticated(user);
((AuthUser)user).getNewToken();
// ...
}