带有http身份验证的Rails ActionCable

时间:2017-11-09 14:19:39

标签: ruby-on-rails actioncable http-auth

我有带ActionCable功能的RoR 5.0.0.1项目。我们通过http authentification进行分区环境。我需要ActionCable来处理我们的暂存。我需要做些什么才能在我们的项目中使用ActionCable和http auth? 我当前的connection.rb文件:

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = find_verified_user
      logger.add_tags 'ActionCable', current_user.email
    end

    protected
    def find_verified_user
      if verified_user = env['warden'].user
        verified_user
      else
        reject_unauthorized_connection
      end
    end

  end
end

我的staging.rb文件:

config.action_cable.url = "ws://user:password@staging.domain.io/cable"
config.action_cable.allowed_request_origins = ['http://user:password@staging.domain.io', 'http://staging.domain.io']

config.middleware.use '::Rack::Auth::Basic' do |u, p|
  [u, p] == ['user', 'password']
end

日志:

2017-11-10T08:15:27.174024+00:00 app[web.1]: [ActionCable] [dev_monkey2@domain.io] TestStatusChannel is streaming from test_status:Z2lkOi8vc2ltcGxlY29kZWNhc3RzLXNhYXMvTW9ua2V5LzIw:Z2lkOi8vc2ltcGxlY29kZWNhc3RzLXNhYXMvRXhwZXJpbWVudC8zM
2017-11-10T08:15:26.985632+00:00 app[web.1]: [ActionCable] [dev_monkey2@domain.io] Registered connection (Z2lkOi8vc2ltcGxlY29kZWNhc3RzLXNhYXMvTW9ua2V5LzIw)
2017-11-10T08:15:27.173731+00:00 app[web.1]: [ActionCable] [dev_monkey2@domain.io] TestStatusChannel is transmitting the subscription confirmation
2017-11-10T08:19:39.432364+00:00 app[web.1]: [ActionCable] [dev_monkey2@domain.io] Finished "/cable/" [WebSocket] for 90.64.96.233 at 2017-11-10 08:19:39 +0000
2017-11-10T08:19:39.432569+00:00 app[web.1]: [ActionCable] [dev_monkey2@domain.io] TestStatusChannel stopped streaming from test_status:Z2lkOi8vc2ltcGxlY29kZWNhc3RzLXNhYXMvTW9ua2V5LzIw:Z2lkOi8vc2ltcGxlY29kZWNhc3RzLXNhYXMvRXhwZXJpbWVudC8zM
2017-11-10T08:46:46.955323+00:00 app[web.1]: [ActionCable] Broadcasting to test_status:Z2lkOi8vc2ltcGxlY29kZWNhc3RzLXNhYXMvTW9ua2V5LzIw:Z2lkOi8vc2ltcGxlY29kZWNhc3RzLXNhYXMvRXhwZXJpbWVudC8zM: 182
2017-11-10T08:46:48.156125+00:00 app[web.1]: [ActionCable] Broadcasting to upload_progress:Z2lkOi8vc2ltcGxlY29kZWNhc3RzLXNhYXMvTW9ua2V5LzIw:Z2lkOi8vc2ltcGxlY29kZWNhc3RzLXNhYXMvRXhwZXJpbWVudC8zM: {:event=>"publish"}

感谢。

0 个答案:

没有答案