AUTH如何与Socketio一起使用?

发布于 2025-01-25 19:27:59 字数 191 浏览 5 评论 0原文

我是Web插座的新手(专门针对Socketio接口,包括客户端和服务器),我想知道通常如何实现基本JWT auth。标题如何通过。我应该将它们添加到每个插座听众或发射事件中吗?或仅在连接期间一次。也许由于连接ID,连接已经被视为私有?

我正在使用socket.io-client v.4和烧瓶插座。 我对一般实践感兴趣,并感谢您的任何信息或您自己的经验。

I'm new to web sockets (specifically to socketIO interfaces, both client and server), and I wonder how basic JWT auth is usually implemented. How the headers are passed through. Should I add them to every socket listener or emit event? Or just once during the connection. Maybe the connection is already considered private because of the connection id?

I am using Socket.io-client v.4 and flask-socketio.
I am interested in general practices and would be grateful for any information or your own experience.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妳是的陽光 2025-02-01 19:27:59

连接时有多种通过身份验证的方法:

  • http头
  • cookie
  • 查询字符串
  • auth

在标题中传递令牌的选项(直接通过WebSocket连接时无效):

const socket = io({
  extraHeaders: {
    "Header-Name": "abcd"
  }
});

同一站点cookie始终传递给服务器。要通过cookie跨站点:

const socket = io("https://my-backend.com", {
  withCredentials: true
});

要在查询字符串中传递:

const socket = io({
  query: {
    token: 'abcd'
  }
});

auth> auth option(socket.io v3及以上)传递它:

const socket = io({
  auth: {
    token: "abcd"
  }
});

There are many ways to pass authentication when you connect:

  • HTTP header
  • Cookie
  • query string
  • auth option

To pass a token in a header (not valid when connecting directly via WebSocket):

const socket = io({
  extraHeaders: {
    "Header-Name": "abcd"
  }
});

Same site cookies are always passed to the server. To pass cookies cross-site:

const socket = io("https://my-backend.com", {
  withCredentials: true
});

To pass it in the query string:

const socket = io({
  query: {
    token: 'abcd'
  }
});

To pass it with the auth option (Socket.IO v3 and up only):

const socket = io({
  auth: {
    token: "abcd"
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文