获取请求在邮递员中工作,但在浏览器中不工作

时间:2019-05-15 11:58:47

标签: php angular authentication cakephp postman

我有一个Web应用程序,该应用程序在客户端使用Angular7,在CakePHP使用Api 我使用的是Basic Authentication,然后将其替换为Token Authentication,所有请求都可以正常工作,只是一个请求仅在Postman中可以正常工作,但在浏览器中却很奇怪,因为似乎认证存在问题,但是不知道原因。

请求应返回一个blob以下载文件,因此我在FPDI中使用CakePHP返回pdf

这是邮递员的要求

邮递员标题

Date →Wed, 15 May 2019 12:02:44 GMT
Server →Apache/2.4.33 (Win32) OpenSSL/1.0.2n PHP/5.6.35
X-Powered-By →PHP/5.6.35
Content-Disposition →inline; filename="doc.pdf"
Cache-Control →private, max-age=0, must-revalidate
Pragma →public
Access-Control-Allow-Origin →*
Keep-Alive →timeout=5, max=100
Connection →Keep-Alive
Transfer-Encoding →chunked
Content-Type →application/pdf

邮递员尸体 Postman Body 在Chrome上请求 Request on Chrome 使用基本身份验证的工作请求 Working request using Basic Auth 使用FireFox

using FireFox

通话请求

    getWorkListPdf(id: number, cem_id?: number) {
    let uri = `${this.workSessionsUrl}workList/${id}`;
    let params = new HttpParams();
    if (cem_id) {
      params = params.set('cemetery_id', cem_id.toString());
    }
    const Auth = localStorage.getItem("AuthenticationToken");
    let header = new HttpHeaders();
      header = header.append("AuthenticationToken", Auth);
    return this.dataService.get(uri,{ headers: header, params: params, responseType: 'arraybuffer'  })
    .pipe(
      map(res => {
                  return res;
                }
      )
    );
  }

非常感谢您的帮助!

3 个答案:

答案 0 :(得分:7)

问题是Api无法识别包含身份验证所需标头信息的请求,我在Authentication中添加了AppController数组以允许同时使用{{1} }和Basic Auth,当收到Token Auth请求时,此请求适用于除此请求之外的所有请求(下载pdf) 我尝试添加

json
再次在Controller中运行

,效果很好!!看来它现在可以识别令牌并返回有效文件!

答案 1 :(得分:1)

如果我完全理解您想要使用承载令牌而不是基本授权进行身份验证。如果需要基本的信息,可以在请求中使用以下标头:

const httpOptions = {
    headers: new HttpHeaders({ 
        'Content-Type': 'application/json',
        'Authorization': 'Basic ' + btoa('username:password')
    })
};

对于持票人授权,可以执行以下操作:

const httpOptions = {
    headers: new HttpHeaders({ 
        'Content-Type': 'application/json',
        'Authorization': 'Bearer ' + JSON.parse(your token)
    })
};

希望有帮助!

答案 2 :(得分:1)

在您的请求中添加 withCredentials true。

const httpOptions = {
    headers: new HttpHeaders({
        'Accept': "application/json",
        'Content-Type': "text/plain;charset=UTF-8",
        'Authorization': 'Bearer ' + JSON.parse(your token)
    }), withCredentials: true
};
相关问题