【问题标题】:How to retrieve custom User object in GAE endpoints?如何在 GAE 端点中检索自定义用户对象?
【发布时间】:2016-05-22 23:12:21
【问题描述】:

我刚刚在我的谷歌应用引擎 Java 应用上创建了自己的自定义身份验证。这并没有像我接下来要做的那样麻烦。

身份验证工作正常,但现在我正在尝试向默认用户对象添加一些额外的字段,这样我就不必对服务器进行如此多的调用。

所以到目前为止,我所做的是创建了一个实现 Authenticator 的自定义类。根据用户是否经过身份验证,authenticate 方法返回 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);
    // ...
}

注意不同的类导入。

我不能在 UserEndpoint 某方法中使用 AuthUser,因为 API 期望我在调用服务器时发布该对象。

如何将额外的数据从验证器传递到我的端点方法?

【问题讨论】:

    标签: java google-app-engine authentication google-cloud-endpoints


    【解决方案1】:

    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。不知道是bug还是功能。

    所以在 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();
    
        // ...
    }
    

    【讨论】:

    • 我可以确认在 Endpoint 方法中使用 com.google.api.server.spi.auth.common.User 有效。
    • 很伤心,但这只有在传入请求中没有正文时才有效。只要请求有正文,就无法编译,因为编译器认为com.google.api.server.spi.auth.common.User 是来自端点的正文:“多个实体参数。(...)”
    • 我快疯了!这个答案很有帮助。
    • AppEngine 文档的链接不再起作用。现在谷歌说[cloud.google.com/endpoints/docs/frameworks/java/…注入的类型是com.google.api.server.spi.auth.common.User。我仍然使用com.google.appengine.api.users.User。有谁知道区别吗?
    • 两个User类之间的区别在[stackoverflow.com/a/40227633/1021892]中解释。最好使用com.google.appengine.api.users.User
    猜你喜欢
    • 1970-01-01
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-08
    • 2013-08-10
    相关资源
    最近更新 更多