简体   繁体   English

Angular HttpClient授权标头已创建但正在消失

[英]Angular HttpClient authorization header created but disappearing

Problem 问题

I am trying to send Request Headers, specifically, an authorization header. 我正在尝试发送请求标头,特别是授权标头。 The authorization Header should look something like this: Authorization: Basic NTY2MTI0Og== In a list of headers. 授权标题应如下所示: Authorization: Basic NTY2MTI0Og==在标题列表中。 Where Basic indicates that it is encoded with base64. Basic表示它使用base64编码。

I'm positive it does get added to the get request made in Angular: Request in Angular. 我很肯定它会被添加到Angular中的get请求中:Angular中的Request。 Although I'm not sure what op is. 虽然我不确定op是什么。 (btw, I'm not yet allowed to embed the image) (顺便说一句,我还没有被允许嵌入图像)

This is what it should look like: From the standard Datasnap Delphi project 这应该是它的样子: 来自标准的Datasnap Delphi项目

This is what mine look like: enter image description here 这就是我的样子: 在这里输入图像描述

As you can see, the Authorization header is missing. 如您所见,缺少Authorization标头。

Errors 错误

Console Error: Screenshot 控制台错误: 屏幕截图

Fiddler Raw request: Screenshot Fiddler Raw请求: 截图

This is how I create the Rest call: 这就是我创建Rest调用的方式:

public authRestCall(auth: string): Observable<string> {
  var headers = new HttpHeaders;
  //HttpHeaders is immutable
  headers = headers.append('Authorization', 'Basic ' + auth);
  return this.http.get<string>(this.localUrl + 'DSAdmin/GetPlatformName', {headers: headers});
}

Alternative 替代

another way I tried doing this was to create an object like this, described in the Angular guide 我尝试这样做的另一种方法是创建一个像Angular指南中描述的这样的对象

const httpOptions = {
  headers: new HttpHeaders({
  'Authorization': 'NTY2MTI0OnVuZGVmaW5lZA=='
  })
};

which I would then add like this: 然后我会添加如下:

return this.http.get<string>(this.localUrl + 'DSAdmin/GetPlatformName', httpOptions;

Delphi 德尔福

In the backend server (A Delphi datasnap module) I have configured CORS like this: 在后端服务器(A Delphi datasnap模块)中,我已经像这样配置了CORS:

Response.setCustomHeader('Access-Control-Allow-Origin','*');
Response.SetCustomHeader('Access-Control-Allow-Headers','*');
Response.SetCustomHeader('Access-Control-Allow-Methods','*');

Versions 版本

- Angular 5 - ionic-angular: 3.9.2 - List item - Delphi RAD studio 10.1 Berlin


Sources that did not solve it for me 没有为我解决的消息来源

Angular 4.3 HTTPClient Basic Authorization not working Angular 4.3 HTTPClient基本授权不起作用

Angular HttpClient not setting Authorization header Angular HttpClient未设置Authorization标头

Angular 4.3 HttpClient doesn't send Authorization header Angular 4.3 HttpClient不发送Authorization标头

Adding “Authorization” header in get request 在get请求中添加“Authorization”标头

I have checked many more questions, but none of them helped me solve the problem. 我已经检查了更多问题,但没有一个能帮我解决问题。 I've been stuck on this for almost a week now. 我已经坚持了近一个星期了。 Maybe I'm not understanding something correctly, but it is not working. 也许我没有正确理解某些东西,但它无法正常工作。

If something is not clear or if I should add more info about something, please let me know. 如果有什么不清楚或者我应该添加更多关于某事的信息,请告诉我。 Thank you for reading. 谢谢你的阅读。


Screenshosts as text Screenshosts as text

Angular headers object: 角度头对象:

headers: Map(0) {}
lazyInit: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: 
Map(0)}
lazyUpdate: Array(1)
  0:
    name: "Authorization"
    op: "a"
    value: "Basic NTY2MTI0OnVuZGVmaW5lZA=="
    __proto__: Object
    length: 1
  __proto__: Array(0)
normalizedNames: Map(0) {}
__proto__: Object

What it should look like: 应该是什么样的:

Request URL: http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName/
Request Method: GET
Status Code: 200 OK
Remote Address: 127.0.0.1:8888
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: *
Access-Control-Allow-Origin: *
Connection: close
Content-Length: 20
Content-Type: text/html; charset=UTF-8
Date: Thu, 18 Oct 2018 10:09:38 GMT
Pragma: dssession=63572.937476.131783,dssessionexpires=1197188
Accept: application/json
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,nl-NL;q=0.8,nl;q=0.7,en-US;q=0.6
Authorization: Basic NTY2MTI0Og==
Cache-Control: max-age=0
Content-Type: text/plain;charset=UTF-8
Host: localhost:8080
If-Modified-Since: Mon, 1 Oct 1990 05:00:00 GMT
Proxy-Connection: keep-alive
Referer: http://localhost:8080/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36

What mine looks like: 我的样子:

Request URL: http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName
Request Method: OPTIONS
Status Code: 401 Unauthorized
Remote Address: [::1]:8080
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: *
Access-Control-Allow-Origin: *
Connection: close
Content-Length: 49
Content-Type: text/html; charset=utf-8
Date: Thu, 18 Oct 2018 13:47:12 GMT
WWW-Authenticate: Basic realm="REST"
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,nl-NL;q=0.8,nl;q=0.7,en-US;q=0.6
Access-Control-Request-Headers: authorization
Access-Control-Request-Method: GET
Connection: keep-alive
Host: localhost:8080
Origin: http://localhost:8100
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36

Console error: 控制台错误:

VM1038:1 OPTIONS http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName 
401 (Unauthorized)
(anonymous) @ VM1038:1

(index):1 Failed to load 
http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName: Response for 
preflight does not have HTTP ok status.

Fiddler Raw: Request Fiddler Raw:请求

OPTIONS http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://localhost:8100
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
Access-Control-Request-Headers: authorization,x-authentication,x-authentication-impersonate
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en;q=0.9,nl-NL;q=0.8,nl;q=0.7,en-US;q=0.6

Response 响应

HTTP/1.1 401 Unauthorized
Connection: close
Content-Type: text/html; charset=utf-8
Content-Length: 49
Date: Thu, 18 Oct 2018 10:06:24 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: *
WWW-Authenticate: Basic realm="REST"
<HTML><BODY><B>401 Unauthorized</B></BODY></HTML>

I fixed it by adding the following piece of code to the TWebModule1.WebModuleBeforeDispatch in Delphi: 我通过将以下代码添加到Delphi中的TWebModule1.WebModuleBeforeDispatch来修复它:

if Trim(Request.GetFieldByName('Access-Control-Request-Headers')) <> '' then
begin
  Response.SetCustomHeader('Access-Control-Allow-Headers', Request.GetFieldByName('Access-Control-Request-Headers'));
  Handled := True;
end;

Source: CORS issue on a Delphi's Datasnap ISAPI Module 来源: Delphi的Datasnap ISAPI模块上的CORS问题

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

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