首页 文章

缺少 Headers aws - 放大角度5.x.

提问于
浏览
2

我正在使用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 验证 . 我觉得我得到了我做错了什么 .

EDIT

Authorization 标头在使用 API.get(...) 时正确设置并向其传递一个标头对象,如:

{
    Authorization: 'myToken'
}

更多信息可在以下链接中阅读aws.github.io/aws-amplify

有什么建议吗?

2 回答

  • 1

    我没有得到你的问题所以我将依赖你的问题的 Headers .

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

    如果您想获取 Headers 并将其放入请求中,则必须公开它 .

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

    默认情况下,Angular只处理几个 Headers 并删除其他 Headers . 您可能没有获得令牌,因为它位于非暴露的标头中 .

  • 0

    我设法找出导致问题的原因 . 错误不是由 Headers 或代码引起的,而是由被调用的路由引起的 . 在 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"
    }
    

    我认为这是由 Headers 引起的 . 对于那些在同样问题上挣扎的人:

    • 确保拨打正确的路线

    • 确保您没有使用 AWS_IAM 授权,但是来自您的 cognito user pool

    • 确保获取当前的 jwt 并将其传递给所有 API 请求,就像我在上面的代码中所示 .

相关问题