我正在开发一个单页网络应用程序。它具有ASP.NET Core 3后端和Angular 9前端。我在 Visual Studio 中的 IIS Express 上运行后端,网址为http://localhost:59280。前端在 Visual Studio Code 中运行,使用ng servehttp ://localhost:4200。之前我不需要在后端开启 CORS,因为我只在 Chrome 中测试了应用程序,添加--disable-web-security命令行参数就足以关闭同源策略。在实时服务器上不需要 CORS,上述跨域情况仅发生在我的开发机器上。
现在我想在 Firefox 中调试前端,但由于无法关闭 Firefox 的同源策略,所以我必须在后端打开 CORS。不幸的是,它不起作用,因为我使用 Windows 身份验证,并且它会停止默认未经身份验证的CORS 预检请求。如果我可以在没有 Windows 身份验证的情况下处理 HTTP OPTIONS 请求,则可以解决此问题。我认为这可以通过在web.config中添加类似的内容来完成:
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true" />
</authentication>
<authorization>
<add accessType="Allow" verbs="OPTIONS" users="*" />
</authorization>
</security>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
...但我收到一条错误消息:“此配置节不能在此路径上使用。当该节被锁定在父级别时,就会发生这种情况。” 显然 web.config 与launchSettings.json冲突,后者似乎在 Visual Studio 中的 IIS Express 上运行后端时控制身份验证,使用以下两行:
{
"iisSettings": …Run Code Online (Sandbox Code Playgroud) 我试图向 REST api 发出请求。这是一个 CORS 请求。\n我的前端:Angular 1.5 (localhost:3000)\n我的后端:Django (*****.ddns.net)
\n\n所以我正在使用一项服务(由不想共享代码的人创建:(),即在真正的请求(预检)之前执行 OPTIONS 请求。准确地说,调用是通过解析选项进行的UI 路由器状态定义。\nDjango 有 CORS 允许 *.
\n\n这是我在 google chrome 中遇到的错误:
\n\nXMLHttpRequest cannot load https://****.net/api/myprofile. The request was redirected to 'https://*****.net/punchclock/api/myprofile/', which is disallowed for cross-origin requests that require preflight.\nRun Code Online (Sandbox Code Playgroud)\n\n如果我在控制器中执行经典的 $http 请求,它就可以工作。
\n\n这是我的 django 收到的请求:
\n\n+6655:5740d0f9:10|\nOPTIONS /punchclock/api//myprofile HTTP/1.0|\nHost:*****.net|\nConnection:close|\nPragma:no-cache|\nCache-Control:no-cache|\nAccess-Control-Request-Method:GET|\nOrigin:http%3a//localhost%3a3000|\nUser-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36|\nAccess-Control-Request-Headers:accept, authorization|\nAccept:*/*|\nReferer:http%3a//localhost%3a3000/dashboard|\nAccept-Encoding:gzip, deflate, sdch|\nAccept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4,es;q=0.2\n-6655:5740d0f9:10\nRun Code Online (Sandbox Code Playgroud)\n\n这是我与邮递员一起执行时得到的响应(当我执行选项请求时,它正在与邮递员一起工作)
\n\nAccess-Control-Allow-Headers \xe2\x86\x92x-requested-with, content-type, accept, origin, authorization, x-csrftoken\nAccess-Control-Allow-Methods \xe2\x86\x92GET, POST, …Run Code Online (Sandbox Code Playgroud) 我在查询我的oauth/token端点时遇到错误.
我已经为我的资源配置了cors,并且还试图允许所有资源,但没有任何工作.
XMLHttpRequest无法加载http:// localhost:8080/oauth/token.对预检请求的响应未通过访问控制检查:请求的资源上不存在"Access-Control-Allow-Origin"标头.原产地" 的http://本地主机:1111 "因此不允许访问.响应具有HTTP状态代码401.
vendor.js:1837 ERROR SyntaxError: Unexpected token u in JSON at position 0
at JSON.parse (<anonymous>)
at CatchSubscriber.selector (app.js:7000)
at CatchSubscriber.error (vendor.js:36672)
at MapSubscriber.Subscriber._error (vendor.js:282)
at MapSubscriber.Subscriber.error (vendor.js:256)
at XMLHttpRequest.onError (vendor.js:25571)
at ZoneDelegate.invokeTask (polyfills.js:15307)
at Object.onInvokeTask (vendor.js:4893)
at ZoneDelegate.invokeTask (polyfills.js:15306)
at Zone.runTask (polyfills.js:15074)
defaultErrorLogger @ vendor.js:1837
ErrorHandler.handleError @ vendor.js:1897
next @ vendor.js:5531
schedulerFn @ vendor.js:4604
SafeSubscriber.__tryOrUnsub @ vendor.js:392
SafeSubscriber.next @ vendor.js:339
Subscriber._next @ vendor.js:279
Subscriber.next @ vendor.js:243
Subject.next @ vendor.js:14989
EventEmitter.emit @ vendor.js:4590
NgZone.triggerError …Run Code Online (Sandbox Code Playgroud) 我正在尝试发出 PUT 请求,但出现以下错误:
Failed to load <localhost:8080/uaa/groups/dfsfaes8323df32>: Response for preflight does not have HTTP ok status.
Run Code Online (Sandbox Code Playgroud)
显然,当我发出 GET 和 POST 请求并在邮递员中工作时它有效,但它不适用于我的代码中的 PUT 请求。
这是我的要求:
let link = "https://ice.<mydomain>.com/uaadev/Groups/{groupId}";
link = link.replace("{groupId}", data);
const updLoad = {
displayName: this.groupInfoObjects.displayName,
description: this.groupInfoObjects.description,
zoneId : "uaa",
schemas: [
"urn:scim:schemas:core:1.0"
]
};
let Header = new Headers({
Authorization: `Bearer {token}`.replace("{token}", this.token),
"Content-Type": "application/json",
Accept: "application/json",
"if-Match":"*"
});
let Option = new RequestOptions({ headers: Header });
this.http.put(link, updLoad, Option).subscribe(
res => {
console.log(res);
console.log(res.status);
if …Run Code Online (Sandbox Code Playgroud) 我有一个非常简单的 FastAPI-Traefik-Docker 设置,您可以在这里找到: https: //github.com/mshajarrazip/fastapi-traefik-docker-cors
\n只需docker-compose up -d运行 FastAPI 和 traefik 服务即可。
此设置中未配置 TLS/SSL。
\n该应用程序有两个端点 GET“/hello”和 POST“/add”,它们除了返回 json 响应之外什么也不做:
\n在app/main.py
import logging\n\nfrom app import routes\nfrom fastapi import FastAPI\nfrom fastapi.middleware.cors import CORSMiddleware\n\ndef create_application() -> FastAPI:\n app = FastAPI(title="\xe2\x9c\xa8 ML API \xe2\x9c\xa8")\n\n app.include_router(routes.hello.router)\n\n # add CORS\n # origins = ["*"]\n app.add_middleware(\n CORSMiddleware,\n allow_origins=["*"], # Allows all origins\n allow_credentials=True,\n allow_methods=["*"], # Allows all methods\n allow_headers=["*"], # Allows all headers\n )\n\n return app\n\n\napp = create_application()\nRun Code Online (Sandbox Code Playgroud)\n … cors ×5
preflight ×5
angular ×2
angular6 ×1
angularjs ×1
asp.net-core ×1
django ×1
docker ×1
fastapi ×1
javascript ×1
rest ×1
spring ×1
spring-mvc ×1
traefik ×1