如何检索自定义用户对象在GAE终点? [英] How to retrieve custom User object in GAE endpoints?

查看:135
本文介绍了如何检索自定义用户对象在GAE终点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚创建了我的谷歌应用程序引擎的Java应用程序自己的自定义验证。正如是我想要做的下一件事是不是太大的麻烦。

I've just created my own custom authentication on my google app engine Java app. And it wasn't that much of a trouble as is the next thing I'm trying to do.

认证工作正常,但现在我想一些额外的字段添加到默认的用户对象,这样我就不用做那么多的电话服务器。

Authentication works fine but now I'm trying to add some additional fields to the default User object so that I wouldn't have to make so many calls to the server.

那么,我到目前为止所做的是创建一个实现身份验证的自定义类。根据用户是否经过验证或不进行身份验证方法将返回用户对象或null。用户对象是然后我的API端点访问。

So what I've done so far is created a custom class that implements Authenticator. Based on whether the user is authenticated or not the authenticate method returns the User object or null. User object is then accessible to my API endpoints.

要延长我的应用程序的功能扩展默认的用户对象,我尝试过,使得一些新的领域,然后将它传递给终端。但是,由于终端访问用户对象是不是同一种对视了一眼,我从我伸出不能得到额外的领域。

To extend my app functionality I've tried extending the default User object, making some new fields, and then passing it to endpoints. However, since the User object accessible by endpoints is not the same kind as the one I extended from I can't get the extra fields.

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);
    // ...
}

的通知不同的类进口。

Notice different class imports.

因为那时API希望我张贴的对象与我的呼叫服务器时,我不能在UserEndpoint做某事方法使用为AuthUser。

I can't use AuthUser in UserEndpoint sth method because then API expects me to post that object with my call to server.

如何从认证额外的数据传递给我的终点的方法?

How can I pass extra data from authenticator to my endpoint method?

推荐答案

AppEngine上文档说注入的类型有以下几种:

AppEngine docs say the injected types are the following:


  • com.google.appengine.api.users.User

  • javax.servlet.http.HttpServletRequest

  • javax.servlet.ServletContext

但是,它没有提到com.google.api.server.spi.auth.common.User,但它的工作原理是肯定的。我只是想用AppEngine上的Java SDK 32年9月1日。我不知道这是否是一个错误或功能。

However, it doesn't mention com.google.api.server.spi.auth.common.User, but it works for sure. I just tried with AppEngine Java SDK 1.9.32. I don't know if it's a bug or feature.

因此​​,在UserEndpoint.java,你必须输入com.google.api.server.spi.auth.common.User,那么你可以将它转换为为AuthUser。

So in UserEndpoint.java, you have to import com.google.api.server.spi.auth.common.User, then you can cast it to 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();

    // ...
}

这篇关于如何检索自定义用户对象在GAE终点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆