如何在不传递引用的情况下在 Cloud Endpoints 中公开用户?

How do I expose a User in Cloud Endpoints without passing around the reference?

Google Cloud Endpoint 中,我可以注入自定义 AuthenticatorUser,如下所示。我想以类似于 Apache ShiroSecurityUtils.getSubject().

的方式将 User 的实例公开给服务方法

我尝试通过执行 UserServiceFactory.getUserService().getCurrentUser() 来做到这一点,但无济于事 (returns null)。

我已经实现了一些在服务上执行身份验证逻辑的注释,我想将其与服务内部的实际业务逻辑分开。因此,如果我可以从任何地方检索属于当前正在处理的请求的当前用户,那将会很有帮助。我在没有 Spring SecurityApache Shiro.

的情况下实现这个

有干净的方法吗?

@ApiMethod(authenticators = {JwtAuthenticator.class}, name = "find", path="foobars", httpMethod = GET)
public Foobar getFoobar(User user, long id) {
    // I'd like to get rid of passing the user reference
    return foobars.get(id, user);  // performs checks on user
}

不幸的是,由于 Endpoints 当前的实现方式,使用 Framework idioms 无法完全做到这一点。如果您停止在您的 API 方法中包含 User 参数,那么身份验证器首先将不会是 运行,这使得实现身份验证器变得毫无用处。

如果您独立于 Endpoints Framework 编写自己的身份验证代码,那么您可以拥有使用 ThreadLocal 变量来存储当前用户的静态帮助程序,这些用户通过 servlet 过滤器或其他机制填充。