0

我有带有 ActionCable 功能的 RoR 5.0.0.1 项目。我们有带有 http 身份验证的暂存环境。我需要 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"}

谢谢。

4

0 回答 0