AWS API Gateway Websocket-Chrome:WebSocket握手期间出错:发送了非空的“ Sec-WebSocket-Protocol”标头,但未收到任何响应

时间:2019-06-11 06:24:39

标签: node.js amazon-web-services google-chrome websocket aws-api-gateway

我正在尝试使用AWS API Gateway的WebSocket功能与Angular Application通信。根据企业要求,我需要使用OAuth令牌保护WebSocket端点。

我已经写了一个自定义的OAuth授权者来验证OAuth令牌。

此自定义授权者与我的REST端点配合良好。

据我了解,浏览器无法支持WebSocket(HTTP headers in Websockets client API)的自定义标头。

为了绕过此限制,我使用Sec-WebSocket-Protocol标头传递OAuth令牌以进行身份​​验证。

(基于查询字符串的身份验证可能不适用于我,因为我会超出URL字符串限制)

我已将我的API网关的配置配置为接受Sec-WebSocket-Protocol作为身份验证标头,并且WSCAT实用程序似乎一切正常。

当我使用RxJS(https://rxjs-dev.firebaseapp.com/api/webSocket/webSocket)将此端点与Angular Client集成时,整个解决方案在FireFox和Edge浏览器中都可以正常工作,但是Chrome会抛出握手异常,即

WebSocket connection to 'wss://xxxx.com/' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received

请求标头:

GET wss://xxxx.com/ HTTP/1.1
Host: xxxx.com
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: http://localhost:4200
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Sec-WebSocket-Key: asdf
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Protocol: asdf

响应标题:

HTTP/1.1 101 Switching Protocols
Connection: upgrade
Date: Tue, 11 Jun 2019 06:15:43 GMT
upgrade: websocket
sec-websocket-accept: asdf
sec-websocket-extensions: permessage-deflate
X-Cache: Miss from cloudfront
Via: 1.1 asdf
X-Amz-Cf-Pop: BOM51
X-Amz-Cf-Id: asdf

2 个答案:

答案 0 :(得分:0)

这是AWS WebSocket API中的一个长期错误

https://forums.aws.amazon.com/thread.jspa?messageID=883536&tstart=0

答案 1 :(得分:0)

很高兴地告诉您,此消息现已添加到API-Gateway中,因此现在您需要更新Lambda函数以返回正确的Sec-WebSocket-Protocol标头: https://docs.aws.amazon.com/apigateway/latest/developerguide/websocket-connect-route-subprotocol.html