简体   繁体   English

如何解决node.JS护照oauth2 cors错误

[英]How to solve node.JS passport oauth2 cors error

I am trying to configure node passport oauth2 login.我正在尝试配置节点护照 oauth2 登录。

If call the get url directly from the browswer, everyhting works perfectly.如果直接从浏览器调用get url,一切正常。

I am trying to set up the login using react client as frontend.我正在尝试使用反应客户端作为前端来设置登录。

react axios api call反应 axios api 呼叫

 const res = await Axios.get(http://locahost:5000/login`)

node.js express node.js 快递

app.use(cors({
  'allowedHeaders': ['sessionId', 'Content-Type', 'authorization'],
  'exposedHeaders': ['sessionId','authorization'],
  // 'origin': true,
  // 'Access-Control-Allow-Origin': 'http://localhost:8080',
  'methods': 'GET,HEAD,PUT,PATCH,POST,DELETE, OPTIONS',
  'preflightContinue': false
}));

app.use(bodyParser.json({ limit: "50mb", extended: true }));

app.get('/npt/login', passport.authenticate('oauth2'));

This is the error I get这是我得到的错误

Access to XMLHttpRequest at 'https://...' (redirected from 'http://localhost:5000/npt/login') from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Do I need to set the headers in the client app?我需要在客户端应用程序中设置标题吗?

UPDATE: I am trying to use this configuration using Azure App Registration.更新:我正在尝试使用 Azure App Registration 来使用此配置。

I have updated the express CORS settings.我已经更新了 express CORS 设置。

UPDATE2: I have managed to solve the issue running the frontend on the same origin as the nodeJS express server. UPDATE2:我已经设法解决了在与 nodeJS express 服务器相同的源上运行前端的问题。 This is the example I have followed: https://github.com/cicorias/react-azuread-passport这是我遵循的示例: https://github.com/cicorias/react-azuread-passport

if you run react side via CRA如果你通过CRA运行 react side

you can add proxy in package.json file您可以在package.json文件中添加代理

like this:像这样:

"proxy": "http://yourTarget.com",

and call http requests from并调用 http 请求

http://localhost:3000

notice : when you change package.json file you must reset npm start command to see the changes注意:当您更改package.json文件时,您必须重置npm start命令以查看更改

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

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