简体   繁体   English

CORS 在 React + Express 应用程序上使用 passport-azure-ad 错误

[英]CORS error using passport-azure-ad on React + Express app

I have a React + Express app for which I am using the passport-azure-ad OIDCStrategy, but I'm having trouble with CORS.我有一个 React + Express 应用程序,我正在使用它的passport-azure-ad OIDCStrategy,但是我在使用 CORS 时遇到了问题。

The error I am receiving is: Access to XMLHttpRequest at 'https://login.microsoftonline.com/...' (redirected from '{SERVER_URL}') from origin '{FRONTEND_URL}' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.我收到的错误是: Access to XMLHttpRequest at 'https://login.microsoftonline.com/...' (redirected from '{SERVER_URL}') from origin '{FRONTEND_URL}' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

My frontend and backend are on different domains (I added a header on the backend to allow the frontend origin).我的前端和后端位于不同的域上(我在后端添加了 header 以允许前端来源)。 I tried adding both urls as Redirect URIs, but that doesn't seem to help.我尝试将两个 url 添加为重定向 URI,但这似乎没有帮助。

Is there a way to set Access-Control-Allow-Origin headers in Azure?有没有办法在 Azure 中设置 Access-Control-Allow-Origin 标头? Or is there a configuration in the React code which I'm missing?还是我缺少的 React 代码中的配置?

Is there a way to set Access-Control-Allow-Origin headers in Azure?有没有办法在 Azure 中设置 Access-Control-Allow-Origin 标头? Or is there a configuration in the React code which I'm missing?还是我缺少的 React 代码中的配置?

Generally, this error occurs when the front end and backend is running on different ports.一般在前端和后端运行在不同的端口时会出现此错误。

The browser will not accept the responses from the backend because of the CORS headers not present.由于CORS标头不存在,浏览器不会接受来自后端的响应。

So, we have to add the CORS headers in the backend request to overcome this issue by using the cors npm package below:因此,我们必须通过使用下面的cors npm包在后端请求中添加 CORS 标头来解决此问题:

var express = require('express')  
var cors = require('cors')  
var app = express()  
app.use(cors())  

It enables CORS headers with all the requests.它为所有请求启用 CORS 标头。
you can refer to cors documentation for more information.您可以参考 cors文档了解更多信息。

Also, We can enable CORS for Azure App Service in azure portal as mentioned here .此外,我们可以在 azure 门户中为 Azure App Service 启用 CORS,如此所述。

You have to use CORS package in the backend and give cros access in your domain like您必须在后端使用 CORS package 并在您的域中提供交叉访问权限,例如

 const cors = require('cors') var corsOptions = { origin: '*', optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204 } app.use(cors(corsOptions));

Don't use fetch from react to api to oauth service, use a link不要使用 fetch 从反应到 api 到 oauth 服务,使用链接

Kindly check out this github issue https://github.com/jaredhanson/passport/issues/734 I hope it helps.请查看此 github 问题https://github.com/jaredhanson/passport/issues/734我希望它有所帮助。

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

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