CORS飞行前响应与实际响应不匹配

时间:2019-03-20 03:42:02

标签: node.js express cors apollo apollo-server

在我的node.js服务器中,我将CORS作为中间件包含在内,如下所示:

app.use(cors({ origin: 'http://<CORRECT_ORIGIN_URL>:3030', credentials: true }))

我在发送请求的应用中使用Apollo Client,并且在初始化ApolloClient时已将凭据设置为“ include”,例如:

// Create a WebSocket link
const wsLink = process.browser ? new WebSocketLink({
    uri: `ws://<CORRECT_REQUEST_URL>:8000/graphql`,
    options: {
        reconnect: true,
    },
}) : null

// Create an http link (use batch, allow cookies response from server)
const httpLink = new BatchHttpLink({
    uri: 'http://<CORRECT_REQUEST_URL>/api/',
    credentials: 'include'
})


// Split terminating link for websocket and http requests
const terminatingLink = process.browser ? split(
    ({ query }) => {
        const { kind, operation } = getMainDefinition(query)
        return kind === 'OperationDefinition' && operation === 'subscription'
    },
    wsLink,
    httpLink,
) : httpLink

// Create Apollo client
const client = new ApolloClient({
    link: ApolloLink.from([authLink, errorLink, terminatingLink])
})

当我尝试登录时,可以看到已发送预检OPTIONS请求并获得了正确的响应:

请求标头(OPTIONS请求)

Access-Control-Request-Headers: content-type
Access-Control-Request-Method: POST
Origin: http://<CORRECT_ORIGIN_URL>:3030
Referer: http://<CORRECT_ORIGIN_URL>/login

响应标题(OPTIONS请求)

Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: content-type
Access-Control-Allow-Methods: GET,HEAD,PUT,PATCH,POST,DELETE
Access-Control-Allow-Origin: http://<CORRECT_ORIGIN_URL>:3030
Connection: keep-alive
Content-Length: 0
Date: Wed, 20 Mar 2019 03:09:14 GMT
Server: nginx/1.15.5 (Ubuntu)
Vary: Origin, Access-Control-Request-Headers
X-Powered-By: Express

但是,当实际的POST请求发送时,我得到以下响应:

响应标题(POST请求)

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/json
Date: Wed, 20 Mar 2019 03:09:15 GMT
Server: nginx/1.15.5 (Ubuntu)
Transfer-Encoding: chunked
Vary: Accept-Encoding, Origin
X-Powered-By: Express

当选项预检显示应该正确时,我不知道为什么响应标题在发布请求中会有所不同。

此错误的POST响应导致客户端上出现以下错误消息:

Access to fetch at 'http://<CORRECT_REQUEST_URL/api/' from origin
'http://<CORRECT_ORIGIN_URL>:3030' has been blocked by CORS policy: 
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'.

我尝试使用谷歌搜索和搜索stackoverflow寻找解决方案,但找不到任何东西。有什么想法吗?

2 个答案:

答案 0 :(得分:2)

好,我知道了... Apollo Server覆盖了cors设置。在.applyMiddleware中添加“ cors:false”会删除替代项。有关更多详细信息,请参见https://www.apollographql.com/docs/apollo-server/api/apollo-server.html#Parameters-2https://github.com/expressjs/cors/issues/134#issuecomment-413543241

答案 1 :(得分:0)

我在Express服务前运行的apache2代理也遇到了类似的问题。代理正在缓存某些(仅某些!)响应。这是我添加到apache配置中的内容,它解决了该问题:

Header set Cache-Control "no-cache, must-revalidate"    env=no-cache-headers
Header set Pragma        "no-cache"                     env=no-cache-headers
Header set Expires       "Sat, 1 Jan 2000 00:00:00 GMT" env=no-cache-headers
相关问题