繁体   English   中英

使用令牌从本地运行的Google Cloud应用程序检索Firebase用户

[英]Retrieve Firebase User using token, from Google Cloud application running locally

我正在研究充当端点API的Java API,并在生产环境中在Google Cloud Platform上运行。 通过将Firebase令牌作为URL的一部分传递来调用API方法,并且该令牌用于创建在API方法内可用的User

@ApiMethod(path = "myPath/{tokenId}/doSomething", httpMethod = "get")
    public ResponseMessage ReturnSomething(@Named("tokenId") String tokenId, User user) throws UnauthorizedException, BadRequestException, InternalServerErrorException, FirebaseAuthException  
    {
        if (user == null)
        ...

在生产中,当从Firebase上的Angular应用程序调用URL并传递URL中的令牌时,将正确创建user 我不完全了解如何通过令牌创建User ,我只知道它作为Firebase与Google Cloud集成的一部分以某种方式“自动”发生。

我想通过在Eclipse内部使用“ 调试方式”>“ App Engine”在本地调试API。 但是,当我这样做并从使用Firebase serve运行的本地Angular应用程序调用API时,令牌已正确传递给本地运行的API,但是user始终为null

@ApiMethod(path = "myPath/{tokenId}/doSomething", httpMethod = "get")
    public ResponseMessage ReturnSomething(@Named("tokenId") String tokenId, User user) throws UnauthorizedException, BadRequestException, InternalServerErrorException, FirebaseAuthException  
    {
        if (user == null)
        // this is always null

我怀疑这是我在本地运行的Java API正确向Firebase进行身份验证时出现的问题。 我查看了本指南该指南建议将Windows上的GOOGLE_APPLICATION_CREDENTIALS属性设置为App Engine默认服务帐户的JSON密钥的路径,这是确保将本地访问权限授予Google Cloud(大概是Firebase)资源。

我已经明确添加了它(无论如何我已经使用命令行运行了gcloud auth application-default login ),但是它仍然无法正常工作。 我只是为user得到null ,没有迹象表明发生了什么。 我不想以编程方式进行身份验证,因为这意味着更改API代码以在调试期间进行不同的身份验证。 在本地以App Engine进行调试时如何检索User

UPDATE

我已经意识到,尽管URL中存在tokenId ,但是在tokenId API时出现以下错误:

WARNING: Authentication failed: com.google.api.auth.UnauthenticatedException: No auth token is contained in the HTTP request

以下代码中的tokenId值是有效值,因此我不确定为什么收到此消息:

@ApiMethod(path = "myPath/{tokenId}/doSomething", httpMethod = "get")
    public ResponseMessage ReturnSomething(@Named("tokenId") String tokenId, User user)

我发现这实际上是Auth0库的问题,该库已在Angular中用于支持对Java API进行身份验证的HTTP请求。 每当从Angular应用程序调用Angular http.get时,使用Auth0库将auth令牌注入到请求标头的Bearer中。 User创建取决于HTTP头中存在的此属性,其值设置为auth令牌的值。

我通过更改此库的配置来解决此问题。 我需要将运行该API的端口(8080)的localhost暂时列入白名单,以允许Auth0在有对localhost:8080的请求时将令牌注入HTTP标头localhost:8080

const jwtConf: JwtModuleOptions = {
  config: {
    tokenGetter: getToken,
    whitelistedDomains: ['localhost:8080']
  }
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM