繁体   English   中英

Chrome扩展程序:向发送cookie凭据(CORS)的node.js服务器发出http请求

[英]Chrome-extension: Making a http request to a node.js server sending in cookie credentials (CORS)

我的node.js服务器出现了一些问题。 我使用heroku启动了它,现在我想要chrome扩展程序将http请求发送到服务器(使用cookie)。

我将express.js用作后端,但无法以正确的方式设置CORS。

这是我在app.js中拥有的:

if (process.env.NODE_ENV !== 'production') {
app.use(cors({
  credentials: true,
  origin: ['http://localhost:4200', 'http://localhost:8000', 'chrome-extension://kdfekbilmpafgiocanhihiogknfilcio']
  }));
} else {
  app.use(cors({
    credentials: true,
    origin: ['chrome-extension://kdfekbilmpafgiocanhihiogknfilcio']
  }));
}

app.use(session({
 secret: 'angular shhh secret auth',
 resave: true,
 saveUninitialized: true,
 cookie : {
   httpOnly: true,
   expires: new Date(253402300000000)
 }
}));

我从chrome扩展程序(客户端)发出这样的请求:

signUp(user) {
  const options = { withCredentials: true };

  return this.myHttp.post(`${this.BASE_URL}/signup`, user, options)
    .toPromise()
    .then((result) => {
      result.json()
  });
}

这是我的错误:

XMLHttpRequest cannot load 
https://moodflowextension.herokuapp.com/loggedin. The value of the 
'Access-Control-Allow-Origin' header in the response must not be the 
wildcard '*' when the request's credentials mode is 'include'. Origin 
'http://localhost:4200' is therefore not allowed access. The 
credentials mode of requests initiated by the XMLHttpRequest is 
controlled by the withCredentials attribute.

我知道我应该以某种方式在标题中传递cookie ...

会话和凭据要求更高的安全性,形式是在标头中不包含通配符。

幸运的是,有一种解决方法。

var whitelist = [/yourDomain/]; var corsOptions = { origin: whitelist }; app.use(cors(corsOptions));

这将使CORS仅允许包含yourDomain(这是一个正则表达式btw,而不是字符串)的源,而不会破坏凭据和会话。

暂无
暂无

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

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