【问题标题】:Missing Headers aws-amplify + angular 5.x缺少标题 aws-amplify + angular 5.x
【发布时间】:2018-08-29 01:07:56
【问题描述】:

我正在使用angular 5.xaws-amplify 构建一个项目。我成功地通过aws-cognito 注册、确认和登录我的用户,现在,我想检索用户的jwt 以将其添加到请求标头以对我的dynamoDb 集合执行CRUD 操作。

不幸的是,当我尝试在发电机上执行此类操作时,我收到以下错误:

{
    "message": "Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=xxxxx"
}

我使用以下 Cognito.service 获取用户的令牌:

import { Injectable } from '@angular/core';

/** rxjs **/
import { fromPromise } from 'rxjs/observable/fromPromise';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';

/** 3rd party **/
import { Auth } from 'aws-amplify';

@Injectable()
export class CognitoService {

    getJwtToken(): Observable<any> {
        return this.getCurrentSession()
            .switchMap((token) => {
                return of(token.getIdToken().getJwtToken());
            })
    }

    private getCurrentSession(): Observable<any> {
        return fromPromise(Auth.currentSession());
    }

}

调用者:

this.cognitoService.getJwtToken()
    .subscribe((token: string) => {
        this.dynamoService.get(
            'testResource?id=someValue',
            {
                Authorization: token
            }
         )
    })

Dynamo.service 如下:

import { Injectable } from '@angular/core';

/** rxjs **/
import { fromPromise } from 'rxjs/observable/fromPromise';
import { Observable } from 'rxjs/Observable';

/** 3rd party **/
import { API } from 'aws-amplify';

/** App Environment **/
import { environment } from '../../../environments/environment';

@Injectable()
export class DynamoDBService {

    apiName = environment.amplify.API.endpoints[0].name;

    get(path: string, headers: any): Observable<any> {
        return fromPromise(
            API.get(
                this.apiName,
                path,
                {
                    headers: headers
                }
            )
        );
    }
}

我的环境如下:

export const environment = {
    production: false,
    amplify: {
        Auth: {
            region: 'eu-central-1',
            identityPoolId: 'eu-central-1:xxx',
            userPoolId: 'eu-central-1_xxx',
            userPoolWebClientId: 'xxxx'
        },
        API: {
            endpoints: [
                {
                    name: "someName,
                    endpoint: "xxxxx"
                }
            ]
        }
    }
};

当然,在我的应用程序启动时,我正在配置放大:

...
/** App Environment **/
import { environment } from '../../environments/environment';
...
Amplify.configure(environment.amplify);
...

在我的api-gatway 上,我启用了标准CORSAuthorization 必需且没有Token 验证。 我觉得我做的一切都很好,我不明白我做错了什么。

编辑

Authorization 标头在使用 API.get(...) 并将标头对象传递给它时得到正确设置:

{
    Authorization: 'myToken'
}

更多内容可以阅读以下链接aws.github.io/aws-amplify

有什么建议吗?

【问题讨论】:

    标签: angular amazon-web-services amazon-dynamodb amazon-cognito aws-amplify


    【解决方案1】:

    我不明白你的问题,所以我将依赖你问题的标题。

    如果缺少标头,那是因为 Angular 在给出响应之前简化了标头。

    如果您想获取标头并将其放入您的请求中,则必须公开它。

    这是通过 Access Control Expose Headers 标头完成的。

    默认情况下,Angular 只处理几个标题并删除其他标题。您可能没有得到您的令牌,因为它位于未公开的标头中。

    【讨论】:

    • 我应该编辑我的问题。没有缺少标题。 aws-amplify API.method 通过传递 { header : someHeaderObj } 正确添加它们。我猜它与aws-amplifyaws-api-gateway 更相关。
    • 我仍然不明白你的问题。你能解释一下吗,
    • 我编辑了我的问题,但我的问题与aws-amplifyaws-api-gateway 非常相关。本质上,没有可用的角度示例,因此我正在查看有关 React 项目的文档和示例。当我尝试执行任何 CRUD 操作时,我得到了我提到的错误,我正在正确添加 Authorization 标头。在github的问题中已经说过添加Authorization标头就足够了,即使错误抱怨其他标头也很困难。
    • 您是否尝试记录您的标头服务器端和 Angular 端?是否已发送,是否已收到?
    • 它是无服务器的,没有服务器端。是的,它们正在被发送和接收。
    【解决方案2】:

    我设法找出导致问题的原因。该错误不是由标头或代码引起的,而是由调用的路由引起的。在api-gateway,可以定义资源和方法。

    我有一个api-gateway 结构,例如:

    /
    ----testResource/
        ----/{id}
            ---- GET
    

    我打电话给testResource?id=someValue 是错误的。通过 id 调用同一资源的正确方法是testResource/someValue

    出于某种原因,网关没有给我一个感知到的错误,而是给了我:

    {
        "message": "Authorization header requires 'Credential' parameter. Authorization header requires 'Signature' parameter. Authorization header requires 'SignedHeaders' parameter. Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header. Authorization=xxxxx"
    }
    

    我认为这通常是由标题引起的。对于那些遇到同样问题的人:

    • 确保您呼叫的路线正确
    • 确保您使用的不是AWS_IAM 授权,而是来自您的cognito user pool 的授权
    • 确保获取当前的 jwt 并将其传递给您的所有 API 请求,就像我在上面的代码中显示的那样。

    【讨论】:

      猜你喜欢
      • 2019-06-27
      • 1970-01-01
      • 2020-10-22
      • 2018-06-15
      • 2020-06-13
      • 2020-08-14
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多