Authentication 验证stomp web套接字客户端

Authentication 验证stomp web套接字客户端,authentication,websocket,stomp,torquebox,Authentication,Websocket,Stomp,Torquebox,我在torquebox上部署了一个RubyonRails应用程序。我需要一些方法来保护我的应用程序中的WebSocket。我使用的是stomp websocket,当用户建立websocket连接时,是否有方法对其进行身份验证?我可以使用用户名和密码参数,但它们当前被忽略。是否有其他方法验证此连接?谢谢 您可以使用会话和存储的令牌对stompet的消息进行身份验证。要使其工作,必须设置Rails以使用Torquebox会话存储。这可以通过初始化器来完成,例如config/initializers

我在torquebox上部署了一个RubyonRails应用程序。我需要一些方法来保护我的应用程序中的WebSocket。我使用的是stomp websocket,当用户建立websocket连接时,是否有方法对其进行身份验证?我可以使用用户名和密码参数,但它们当前被忽略。是否有其他方法验证此连接?谢谢

您可以使用会话和存储的令牌对stompet的消息进行身份验证。要使其工作,必须设置Rails以使用Torquebox会话存储。这可以通过初始化器来完成,例如
config/initializers/torquebox\u init.rb

AppName::Application.config.session_store :torquebox_store
现在Stomplet将可以访问会话。下面是一个示例,它使用会话参数
:authentication\u token
匹配数据库中用户的authentication\u token。将检查身份验证令牌的订阅、发送消息和取消订阅:

require 'torquebox-stomp'

class StompletDemo

  def initialize()
    super
    @subscribers = []
  end

  def configure(stomplet_config)
  end

  def on_message(stomp_message, session)
    token = session[:authentication_token]

    if is_authenticated?( token )
      @subscribers.each do |subscriber|
        subscriber.send( stomp_message )
      end
    end
  end

  def on_subscribe(subscriber)
    session = subscriber.session
    if is_authenticated?(session[:authentication_token])
      @subscribers << subscriber
    end
  end

  def on_unsubscribe(subscriber)
    session = subscriber.session
    if is_authenticated?(session[:authentication_token])
      @subscribers.delete( subscriber )
    end
  end

  def is_authenticated?(token)
    User.where( authentication_token: token ).exists?
  end

end

对于其他有这个问题的人,我就是这样解决的

基本上,代币系统对我来说没有规模,特别是因为我使用Desive。 如果你想把你的websocket托管在chrome扩展中,只需将用户名/密码直接传递给stomp,并让它在stomplet中管理自己的虚拟订阅者会话就很容易了。这也可以让你做一些有趣的事情,因为你是推给谁

 # user has successfully authenticates
 session[:authentication_token] = @user.authentication_token