【问题标题】: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'.-当请求的凭据模式为“包含”时,响应中的“Access-Control-Allow-Origin”标头的值不能是通配符“*”。-
【发布时间】:2021-11-18 18:22:30
【问题描述】:

我正在尝试使用带有 graphql api 的 nodejs 连接我的 Angular 应用程序,并且我正在使用 cookie/会话进行身份验证。

该 api 与 GraphQL PLayground 完美配合。 这是我与 graphql api 连接的角度连接。

const uri = "http://localhost:4000/graphql/"; // <-- add the URL of the GraphQL server here

export function createApollo(httpLink: HttpLink): ApolloClientOptions<any> {
  return {
    link: httpLink.create({ uri, withCredentials: true }),
    cache: new InMemoryCache(),
  };
}

如果我设置了withCredentials:false,连接就会建立,但我不能再使用cookies进行身份验证了。


这是我在 cors 中间件上的代码。

    const corsOptions = {
      credentials: true,
      origin: "http://localhost:4200",
    };
    app.use(cors(corsOptions));

我已经尝试了以下代码 sn-ps 来完成这项工作,但没有成功。

  app.use(function (req, res, next) {
      res.header("Access-Control-Allow-Origin", "http://localhost:4200");
      res.header(
        "Access-Control-Allow-Headers",
        "Origin, X-Requested-With, Content-Type, Accept",
      );
      res.header("Access-Control-Allow-Credentials", "true");
      next();
    });
    const corsOptions = {
      credentials: true,
      origin: true,
    };
    app.use(cors(corsOptions));

这是正在显示的确切错误。


我已尝试以下问题链接中的所有解决方案。
相关问题:

...和类似的帖子,但没有解决这个问题。

【问题讨论】:

    标签: node.js angular express cors session-cookies


    【解决方案1】:

    您必须在创建服务器时设置apolloServer.applyMiddleware({ app, cors: false });,否则应用程序中似乎有两个 cors 阻止程序,其中任何一个都会抛出任何尝试连接的客户端,

    【讨论】:

      【解决方案2】:

      要么使用 app.use(cors(....)) 将 cors 设置添加到顶部,要么在添加 apolloServer 作为中间件时添加它。

      【讨论】:

      • 你说得对,我在其中写了两个 cors 的代码同时阻塞了我的 ping。
      猜你喜欢
      • 2018-11-09
      • 2018-07-31
      • 2018-12-16
      • 2018-07-29
      • 2020-11-10
      • 1970-01-01
      • 2018-10-09
      • 2017-08-24
      • 2018-01-13
      相关资源
      最近更新 更多