AWS API 网关 Websocket - Chrome:WebSocket 握手期间出错:发送非空'Sec-WebSocket-Protocol'标头,但未收到响应



我正在尝试使用 AWS API Gateway 的 WebSocket 功能与我的 Angular 应用程序进行通信。根据企业要求,我需要使用 OAuth 令牌保护我的 WebSocket 终结点。

我编写了一个自定义的 OAuth 授权方来验证 OAuth 令牌。

此自定义授权方在我的 REST 端点上工作正常。

根据我的理解,浏览器不能支持 WebSocket 的自定义标头(Websockets 客户端 API 中的 HTTP 标头)。

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

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

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

当我使用 RxJS (https://rxjs-dev.firebaseapp.com/api/webSocket/webSocket) 将此端点与我的 Angular 客户端集成时,整个解决方案在 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

这是 AWS WebSocket API 中长期存在的错误

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

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

相关内容

最新更新