经过几年的停顿,我正在尝试开发Web,而CORS似乎已走到了尽头。
为什么
<script type="module" src="./index.js"></script>
Run Code Online (Sandbox Code Playgroud)
导致CORS错误?我正在加载要加载另一个本地文件的本地文件(file://)。我不明白为什么CORS甚至会抱怨,毕竟它们都是同一个起源。
我有2台服务器。一个在localhost:5555上托管 next.js 应用程序,另一个在localhost:4444上托管 api 的快速服务器。
身份验证 api 返回一个 cookie,但是这不是在localhost:5555上运行的浏览器中设置的。
res.cookie('cokkieName', 'bob', {
domain: '127.0.0.1:5555',
maxAge: 900000,
httpOnly: true,
});
res.status(200).json({
session: jwtSigned,
role: 'default',
});
Run Code Online (Sandbox Code Playgroud)
我的 cors 设置是:
const options: cors.CorsOptions = {
allowedHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept', 'X-Access-Token', 'Authorization'],
credentials: true,
methods: 'GET,HEAD,OPTIONS,PUT,PATCH,POST,DELETE',
origin: 'http://localhost:5555',
preflightContinue: false,
};
Run Code Online (Sandbox Code Playgroud)
我更喜欢使用 api 而不是通过 next.js 设置 cookie。
我试过交替 cors 设置但没有成功。客户端调用使用 axios 并且已经withCredentials设置。
我正在构建 .Net 5 Web API,并且仅当我在本地(也称为本地主机)运行时才会遇到 CORS 问题。当我将应用程序部署到 Azure 后,我可以从 Blazor 应用程序正常访问我的 API。
类Startup.cs
readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
Run Code Online (Sandbox Code Playgroud)
类:Startup.cs
方法:ConfigureServices
services.AddCors(options =>
{
//didn't work
//options.AddDefaultPolicy(builder =>
//{
// builder.SetIsOriginAllowed(origin => new Uri(origin).Host == "localhost");
//});
options.AddPolicy(name: MyAllowSpecificOrigins,
builder =>
{
builder.WithOrigins("https://localhost:44373/", "https://myawesomesite")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
Run Code Online (Sandbox Code Playgroud)
类:Startup.cs
方法:Configure
app.UseCors(MyAllowSpecificOrigins);
Run Code Online (Sandbox Code Playgroud) 感谢评论中的 Freakish解决了localhost:3030问题是我的端点正在使用而不是http://localhost:3030
Node 运行在端口 3030 上,ionic 运行在端口 8100 上,所以我知道这是问题所在localhost:3030,localhost:8100并且已经寻找了所有实现和允许 CORS 的方法。
我已经尝试了一切,从安装和使用 cors 中间件到遵循其他实施 CORS 的指南。在下面的代码中,我尝试按照指南制作自定义中间件,但即使这样也不起作用。
旁注 socket.io 工作得很好。
app.use(bodyParser.urlencoded({'extended':'true'}));
app.use(bodyParser.json({ limit: '5mb' }));
// enable CORS
app.use(function( req, res, next ) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "x-requested-with, content-type");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
res.header("Access-Control-Allow-Credentials", "true");
res.header("Access-Control-Max-Age", "1000000000");
if ('OPTIONS' == req.method) { res.send(200); } else { next(); }
});
io.on('connection', (socket) => {
socketIo.process(socket, io);
});
app.post('*', (req, res) => {
post.process(req, res);
});
http.listen(port, …Run Code Online (Sandbox Code Playgroud) 我有一个用于通过 CDK 部署的 api 的 OpenAPI 规范。规范看起来像:
openapi: 3.0.1
info:
title: My API
description: My REST API with CORS enabled
version: 0.1.0
x-amazon-apigateway-cors:
allowOrigins:
- "*"
allowCredentials: true
exposeHeaders:
- "x-apigateway-header"
- "x-amz-date"
- "content-type"
maxAge: 3600
allowMethods:
- "*"
allowHeaders":
- "x-apigateway-header"
- "x-amz-date"
- "content-type"
- "Authorization"
components:
securitySchemes:
lambda:
type: "apiKey"
name: "Authorization"
in: "header"
x-amazon-apigateway-authtype: "custom"
x-amazon-apigateway-authorizer:
authorizerUri: "{{my-lambda-authorizer}}"
authorizerResultTtlInSeconds: 300
type: "token"
paths:
/user/{id}:
get:
summary: Get info of specified user.
parameters:
- in: path …Run Code Online (Sandbox Code Playgroud) amazon-web-services aws-cloudformation aws-lambda aws-api-gateway aws-cdk
javascript ×3
cors ×2
node.js ×2
.net-core ×1
aws-cdk ×1
aws-lambda ×1
c# ×1
express ×1
next.js ×1
reactjs ×1