nginx 如何在deno WebSocket中设置cookie

yk9xbfzb  于 5个月前  发布在  Nginx
关注(0)|答案(1)|浏览(68)

如何在WebSocket连接中设置cookie?我想这样做是因为我使用nginx ingress session affinity将用户的会话与特定的pod相关联。但是这个请求是在无服务器函数(即deno)中发出的。我已经阅读了以下内容:

但不幸的是,这些建议的解决方案似乎对我不起作用。具体来说,在url参数中设置affinity token或让浏览器自动设置它。更多的上下文,这里是我的代码中进行此调用的部分:

const wsUrl = `wss://${this.username}:${this.password}@${this.host}/api/${this.id}`
this.logger.info(`Connecting to WebSocket URL: ${wsUrl}`);
this._ws = new StandardWebSocketClient(wsUrl);

字符串
相关入口控制器的配置如下:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-kernel-gateway
  namespace: default
  annotations:
    cert-manager.io/issuer: letsencrypt-nginx
    nginx.ingress.kubernetes.io/auth-type: basic
    nginx.ingress.kubernetes.io/auth-secret: basic-auth
    nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - wovenmind'
    nginx.ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/session-cookie-name: "cookie-affinity"
    nginx.ingress.kubernetes.io/session-cookie-hash: "sha1"
    nginx.ingress.kubernetes.io/affinity-mode: persistent
spec:
  rules:
    - host: host.com
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: service
                port:
                  number: 8888
  ingressClassName: nginx
  tls:
  - hosts:
    - host.com
    secretName: letsencrypt-private-key


问题是,似乎没有办法配置入口容器来使用参数中的affinity token,也没有办法(我知道)像你在浏览器中看到的那样自动/全局设置header。
是我弄错了吗,还是在deno中无法通过WebSocket连接传递cookie?

6qqygrtg

6qqygrtg1#

请参阅以下内容以获得对该问题的良好概述:https://stackoverflow.com/a/77060459/13752922
另请参阅Deno中一个可能有用的实用程序:WebSocketStream。

  1. https://developer.chrome.com/articles/websocketstream/
  2. https://deno.land/[email protected]?s=WebSocketStream&unstable=
    WebSocketStream有一个类似于new WebSocketStream(url, { headers })的签名,但我不确定它是否适用于您的用例。
    最后,如果你使用的是Kubernetes集群,您应该考虑在当前WebSocket实现和当前服务器之间创建一个代理服务。(Ingress + Service),接受某种格式的Sec-WebSocket-Protocol报头(可在标准WebSocket中配置,如new WebSocket(url, [...protocols]))这样您就可以获得所需的标头,然后它将调用者代理到目标服务。例如,考虑以下Sec-WebSocket-Protocol标头:[“some-protocol”,“cookie| {cookie value}"],然后在代理上检查此标头,然后在后续请求中将标头设置为目标服务。请记住,“some-protocol”是必需的,即使您不对协议执行任何操作,也需要在返回中包含它。

相关问题