【发布时间】:2021-01-26 15:30:34
【问题描述】:
这只是另一个 CORS 问题。该设置是用 ASP.NET Core 2.1 编写的 AWS Lambda 函数。在 ASP.NET Core 后端启用 CORS:
public IServiceProvider ConfigureServices(IServiceCollection services)
{
...
options.AddPolicy("DeveloperCors", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
}
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IActionDescriptorCollectionProvider actionDescriptorProvider)
{
...
app.UseCors("DeveloperCors");
...
}
客户端应用程序使用 Angulars common/http 和 Authentication 标头中的不记名令牌进行查询。
对服务器的第一个请求是带有以下标头的 OPTIONS 请求:
Accept: */*
Accept-Encoding gzip, deflate, br
Accept-Language de,en-US;q=0.7,en;q=0.3
Access-Control-Request-Headers: authorization,content-type
Access-Control-Request-Method: POST
Connection: keep-alive
Host: devperts-test2.d-velop.cloud
Origin: http://localhost:4200
Referer: http://localhost:4200/
TE: Trailers
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0
答案是:
access-control-allow-credentials: true
access-control-allow-headers: authorization,content-type
access-control-allow-origin: http://localhost:4200
content-security-policy: frame-ancestors 'self'
date: Mon, 12 Oct 2020 09:58:56 GMT
server: openresty/1.15.8.3
vary: Origin
x-amz-apigw-id: XXXXX
x-amzn-requestid: XXXXX
x-amzn-trace-id: XXXXX
X-Firefox-Spdy: h2
x-frame-options: SAMEORIGIN
在 AWS 日志中,我看到响应来自我的应用程序。然后客户端发送一个 POST 请求:
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: de,en-US;q=0.7,en;q=0.3
Authorization: Bearer XXXXX
Connection: keep-alive
Content-Length: 212
Content-Type: application/json
Host: devperts-test2.d-velop.cloud
Origin: http://localhost:4200
Referer: http://localhost:4200/
TE: Trailers
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0
服务器回答:
content-encoding: gzip
content-security-policy: frame-ancestors 'self'
content-type: text/plain
date: Mon, 12 Oct 2020 09:58:56 GMT
server: openresty/1.15.8.3
vary: Accept-Encoding
X-Firefox-Spdy: h2
x-frame-options: SAMEORIGIN
此响应是从 AWS API Gateway 生成的,因为 CloudWatch 日志中没有条目。在浏览器的控制台窗口中,我看到以下消息:
Cross-source (cross-origin) request blocked: The same source rule prohibits reading the external resource on https://devperts-test2.d-velop.cloud/XXXX. (Reason: CORS header 'Access-Control-Allow-Origin' is missing).
这是由 deepl 从德语翻译过来的。
有一些技巧,例如在 AWS 功能中激活 cors,但没有任何效果。
慢慢地我觉得我太愚蠢了。也许这里的其他人有小费给我?
【问题讨论】:
标签: angular amazon-web-services asp.net-core cors